搜索
系统检测到您的用户名不符合规范:

TP5做公众号第三方平台的时候会遇到的问题解答

浏览:2287 发布日期:2018年02月12日 分类:用法示例 关键字: Easywechat 微信第三方平台
昨天上午突然想把自己公众号上的一些小应用移植到其他公众号,最好的方法肯定是第三方了。 然而easywechat4.0文档中是没有针对thinkphp的,在搞了一天尝试和改动之后,终于完成了第三方授权的开发。
昨天上午突然想把自己公众号上的一些小应用移植到其他公众号,最好的方法肯定是第三方了。 然而easywechat4.0文档中是没有针对thinkphp的,在搞了一天尝试和改动之后,终于完成了第三方授权的开发。 当然,本篇同样是根据网友Nixus的那一片文章收到的启发,并做了一些小调整:https://www.easywechat.com/discussions/153 因为是才通过检测,到底对不对也只有上线了才知道。如果哪位大神能够在文末给出简洁操作的话,小人不胜感激!

1. 第三方平台授权 $openPlatform = Factory::openPlatform($config);
$server = $openPlatform->server;
$message = $server->getMessage();
switch ($message['InfoType']) {
case Guard::EVENT_AUTHORIZED:
// 处理授权成功事件
$server->push(function ($message) {
// ...
}, Guard::EVENT_AUTHORIZED);
break;
case Guard::EVENT_UPDATE_AUTHORIZED:
// 处理授权更新事件
$server->push(function ($message) {
// ...
}, Guard::EVENT_UPDATE_AUTHORIZED);
break;
case Guard::EVENT_UNAUTHORIZED:
// 处理授权取消事件
$server->push(function ($message) {
// ...
}, Guard::EVENT_UNAUTHORIZED);
break;
}
$server->serve()->send();
2. 第三方平台上线前的检查代码 // 1.准备所有需要配置的东西
$openPlatform = Factory::openPlatform($config);
$server = $openPlatform->server;
$msg = $server->getMessage();

// 2.获取公众号信息所需要的信息
$gh_id = $msg['ToUserName'];
$openid = $msg['FromUserName'];
$app = Db::name('op_app')->where('username', $gh_id)->find();

// 3.通过接口把公众号的所有信息搞下来
$authinfo = $openPlatform->getAuthorizer($app['appid'])['authorization_info'];
$authappid = $authinfo['authorizer_appid'];
$authtoken = $authinfo['authorizer_refresh_token'];

// 第三方测试用的
switch ($msg['MsgType']) {
case 'event':
$cuservice = $openPlatform->officialAccount($authappid, $authtoken)->customer_service;
$cuservice->message($msg['Event'] . 'from_callback')
->from($gh_id)
->to($openid)
->send();
case 'text':
$cuservice = $openPlatform->officialAccount($authappid, $authtoken)->customer_service;
$word = $msg['Content'];
if ($word == 'TESTCOMPONENT_MSG_TYPE_TEXT') {
$cuservice->message($word . '_callback')
->from($gh_id)
->to($openid)
->send();
} elseif (strpos($word, 'QUERY_AUTH_CODE') == 0) {
echo '';
$code = substr($word, 16);
$authorizerInfo = $openPlatform->handleAuthorize($code)['authorization_info'];
$cuservice = $openPlatform->officialAccount($authorizerInfo['authorizer_appid'], $authorizerInfo['authorizer_refresh_token']);
$cuservice->customer_service->message($code . "_from_api")
->from($gh_id)
->to($openid)
->send();
}
}
收藏
daneas
积分:1285 等级:LV3
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /