微信第三方平台开发包
- 实现公众号授权和绑定
- 代公众号实现业务
- 代公众号使用JS SDK
- 第三方平台事件通知处理
- 实现公众号全网发布接入检测的自动化测试代码
访问网址
- 官网地址
- Github地址
简单使用说明
- 假设配置网站根目录为/data/wx_component/
- 网站目录结构为/data/wx_component/example/和/data/wx_component/src/
- 假设网站域名为www.xxx.com
- 配置公众号第三方平台参数
- 把配置的第三方平台参数配置/example/config.php
- 配置完成后,进行全网发布接入检测。
- 如果不成功,检查以下几项
- 检查/example/目录是否可写
- 检查/example/cache/目录的日志文件,查看原因
- 检查apache rewrite模块是否开启,是否支持.htaccess
- 发布成功后,用浏览器打开example的php样例,检测授权、js_sdk功能。
PHP全网发布自动化测试代码如下
<?php
/**
* 授权后代替公众号实现业务
* @author lv_fan2008@sina.com
*/
include_once "config.php";
include_once dirname(dirname(__FILE__)) . "/src/WxComponentService.class.php";
$cfg = get_wx_config();
if (!$cfg) {
die("no access!");
}
$wxComponentConfig = $cfg['component_cfg'];
$wxComponentService = new WxComponentService($wxComponentConfig, new FileCache($GLOBALS['cacheDir']));
$appId = $cfg['app_id'];
// 如果为全网发布接入检测的专用测试公众号,转入自动化测试代码
if ($appId == 'wx570bc396a51b8ff8') {
test_auto_case($wxComponentService, $appId);
exit;
}
// 正常业务处理
$weObj = $wxComponentService->getWechat($appId);
$ret = $weObj->valid(true);
if ($ret === false) {
log_ex('wx_auth_msg', "appId:{$appId}, auth valid failed! param:" . print_r($_GET, true));
die('no access');
} else if ($ret !== true) {
log_ex('wx_auth_msg', "appId:{$appId}, only die echostr:" . $ret);
die($ret);
}
$weObj->getRev();
log_ex('wx_auth_msg', "appId:{$appId} receive data:" . $weObj->getRevData());
$weObj->text("success")->reply('', true); // 简单测试回复success
/**
* 得到地址对应的配置信息以及appId
* @return bool
*/
function get_wx_config()
{
$url = is_https() ? "https://" : "http://" . $_SERVER['HTTP_HOST']
. ($_SERVER["SERVER_PORT"] != 80 ? ":" . $_SERVER["SERVER_PORT"] : "")
. $_SERVER['REQUEST_URI'];
$url_param = parse_url($url);
$url_path_arr = explode("/", $url_param['path']);
foreach ($GLOBALS['wxComponentConfig'] as $wxComponentConfig) {
$cfg_url_param = parse_url($wxComponentConfig['receive_app_event_url']);
if ($url_param['scheme'] == $cfg_url_param['scheme'] && $url_param['host'] == $cfg_url_param['host']
&& $url_param['port'] == $cfg_url_param['port']
) {
$cfg_path_arr = explode("/", $cfg_url_param['path']);
$appId = "";
foreach ($cfg_path_arr as $i => $v) {
if ($v != '$APPID$' && $v != $url_path_arr[$i]) {
$appId = "";
break;
} elseif ($v == '$APPID$') {
$appId = $url_path_arr[$i];
}
}
if ($appId) {
return array('component_cfg' => $wxComponentConfig, 'app_id' => $appId);
}
}
}
return false;
}
function is_https()
{
if (!isset($_SERVER['HTTPS']))
return FALSE;
if ($_SERVER['HTTPS'] === 1) { //Apache
return TRUE;
} elseif ($_SERVER['HTTPS'] === 'on') { //IIS
return TRUE;
} elseif ($_SERVER['SERVER_PORT'] == 443) { //其他
return TRUE;
}
return FALSE;
}
/**
* 全网发布接入检测自动化测试代码
* @param WxComponentService $wxComponentService
* @param string $appId
*/
function test_auto_case(&$wxComponentService, $appId)
{
$weObj = $wxComponentService->getWechat($appId);
$ret = $weObj->valid(true);
if ($ret === false) {
log_ex('wx_auth_msg', "appId:{$appId}, auth valid failed! param:" . print_r($_GET, true));
die('no access');
} else if ($ret !== true) {
log_ex('wx_auth_msg', "appId:{$appId}, only die echostr:" . $ret);
die($ret);
}
$weObj->getRev();
log_ex('wx_auth_msg', "appId:{$appId} receive data:" . $weObj->getRevData());
if ($weObj->getRevType() == Wechat2::MSGTYPE_TEXT) {
$recv_txt = $weObj->getRevContent();
if ($recv_txt == 'TESTCOMPONENT_MSG_TYPE_TEXT') {
log_ex('wx_auth_msg', "test_auto_case send TESTCOMPONENT_MSG_TYPE_TEXT_callback");
$weObj->text('TESTCOMPONENT_MSG_TYPE_TEXT_callback')->reply('', false);
exit;
} else if (preg_match('#QUERY_AUTH_CODE:(.*)#', $recv_txt, $matches)) {
log_ex('wx_auth_msg', "test_auto_case send QUERY_AUTH_CODE");
$weObj->text('')->reply('', false);
$ret = $wxComponentService->authorizeCallbackProcess($matches[1], 10);
if ($ret['code'] != 0) {
log_ex('wx_auth_msg', 'test_auto_case authorizeCallbackProcess failed! ret:' . print_r($ret, true));
exit;
}
$weObj->access_token = $wxComponentService->getAppAccessToken($ret['appAcountInfo']['authorization_info']['authorizer_appid']);
$res_arr = array('touser' => $weObj->getRevFrom(), 'msgtype' => 'text', 'text' => array('content' => $matches[1] . "_from_api"));
log_ex('wx_auth_msg', 'test_auto_case sendCustomMessage data:' . print_r($res_arr, true));
$ret = $weObj->sendCustomMessage($res_arr);
log_ex('wx_auth_msg', 'test_auto_case sendCustomMessage: ret:' . print_r($weObj, true));
if (!$ret) {
log_ex('wx_auth_msg', 'test_auto_case sendCustomMessage failed');
}
exit;
}
} else if ($weObj->getRevType() == Wechat2::MSGTYPE_EVENT) {
$data = $weObj->getRevData();
$ev = $data['Event'];
log_ex('wx_auth_msg', 'test_auto_case send ' . $ev . "from_callback");
$weObj->text($ev . "from_callback")->reply('', false);
log_ex('wx_auth_msg', 'test_auto_case send ' . $ev . "from_callback done.");
} else {
die("no access");
}
exit;
}