WordPress Customize Plugin – 微信客製外掛-Token認證
建立微信 ( Weixin ) token verified 的回覆頁面, 要先安裝 plugin: wp-router
- 建立轉向的網址: wpwxtoken & 對應處理的 function: wpwxtoken_route_callback
add_action( 'wp_router_generate_routes', 'add_wpwxtoken_route', 20 ); function add_wpwxtoken_route( $router ) { $route_args = array( 'path' => '^wpwxtoken', 'query_vars' => array( ), 'page_callback' => 'wpwxtoken_route_callback', 'page_arguments' => array( ), 'access_callback' => true, 'title' => __( 'weixin token Route' ), 'template' => array( 'page.php', dirname( __FILE__ ) . '/page.php' ) ); $router->add_route( 'wpwxtoken-route-id', $route_args ); }
- 設定 function: wpwxtoken_route_callback 要處理的程式檔
// 轉到這個頁面: /admin/wx-token.php function wpwxtoken_route_callback( ) { include (WPWX_PLUGIN_DIR . '/admin/wx-token.php'); }
- wx-token.php : 微信 token 驗證的程式處理
<?php /** * 驗證微信的 token */ $path = $_SERVER['DOCUMENT_ROOT']; include_once $path . '/wp-config.php'; include_once $path . '/wp-load.php'; include_once $path . '/wp-includes/wp-db.php'; include_once $path . '/wp-includes/pluggable.php'; // 由微信平台自已設定的 token 值 $token = get_option( 'wpwx_Token'); // 由微信平台 post 送來的參數 $signature =isset($_GET["signature"])?$_GET["signature"]:''; $timestamp =isset($_GET["timestamp"])?$_GET["timestamp"]:''; $nonce = isset($_GET["nonce"])?$_GET["nonce"]:''; $echostr = isset($_GET['echostr'])?$_GET["echostr"]:''; $post_data = "{'signature':$signature, 'timestamp:', $timestamp, 'nonce:': $nonce, 'echostr': $echostr }"; //組合參數作 shal 運算 $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr,SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); //驗證 if( $tmpStr == $signature ){ echo $echostr; }else{ //wp_send_json_error( array('code' => 500, 'data' => $post_data, 'msg' => '錯誤的請求') ); wp_redirect(home_url()); } exit(); ?>
- wx-token.php : 微信 token 驗證的程式處理, 改用 easywechat 的作法
<?php /** * 驗證微信的 token */ $path = $_SERVER['DOCUMENT_ROOT']; include_once $path . '/wp-config.php'; include_once $path . '/wp-load.php'; include_once $path . '/wp-includes/wp-db.php'; include_once $path . '/wp-includes/pluggable.php'; define( 'WPWX_PLUGIN', __FILE__ ); define( 'WPWX_PLUGIN_BASENAME', plugin_basename( WPWX_PLUGIN )); define( 'WPWX_PLUGIN_NAME', trim( dirname( WPWX_PLUGIN_BASENAME ), '/' ) ); define( 'WPWX_PLUGIN_DIR', untrailingslashit( dirname( WPWX_PLUGIN ) ) ); define( 'APP_ROOT_DIR', substr(WPWX_PLUGIN,0,stripos(WPWX_PLUGIN,"wp-content")-1) ); require_once WPWX_PLUGIN_DIR . '/vendor/autoload.php'; use EasyWeChat\Factory; $ewcConfig= (include WPWX_PLUGIN_DIR . '/includes/EasyWeChat-config.php'); $app= Factory::officialAccount($ewcConfig); $server = $app->server; $app->server->push(function ($message) { return get_option( 'wpwx_Welcome'); }); $response = $app->server->serve(); $response->send(); exit; ?>
- EasyWeChat-config.php 程式碼
<?php defined( 'ABSPATH' ) or die( 'You cannot be here.' ); return [ /** * 账号基本信息,请从微信公众平台/开放平台获取 */ 'app_id' => get_option( 'wpwx_AppID'), // AppID 'secret' => get_option( 'wpwx_AppSecret'), // AppSecret 'token' => get_option( 'wpwx_Token'), // Token 'aes_key' => get_option( 'wpwx_AesKey'), // EncodingAESKey,兼容与安全模式下请一定要填写!!! /** * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 * 使用自定义类名时,构造函数将会接收一个 `EasyWeChat\Kernel\Http\Response` 实例 */ 'response_type' => 'array', /** * 日志配置 * * level: 日志级别, 可选为: * debug/info/notice/warning/error/critical/alert/emergency * path:日志文件位置(绝对路径!!!),要求可写权限 */ 'log' => [ 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod 'channels' => [ // 测试环境 'dev' => [ 'driver' => 'single', 'path' => WPWX_PLUGIN_DIR .'/logs/easywechat.log', 'level' => 'debug', ], // 生产环境 'prod' => [ 'driver' => 'daily', 'path' => WPWX_PLUGIN_DIR .'/logs/easywechat.log', 'level' => 'info', ], ], ], /** * 接口请求相关配置,超时时间等,具体可用参数请参考: * http://docs.guzzlephp.org/en/stable/request-config.html * * - retries: 重试次数,默认 1,指定当 http 请求失败时重试的次数。 * - retry_delay: 重试延迟间隔(单位:ms),默认 500 * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php */ 'http' => [ 'max_retries' => 2, 'retry_delay' => 500, 'timeout' => 5.0, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri ], ];
你必須 登入 才能發表評論。