This SDK is developed based on EasyWechat. In order to facilitate the development of third-party platforms for WeChat public accounts, it encapsulates the development of third-party platforms authorized by WeChat public accounts, as well as automated network-wide release access. All functions have been tested by the existing public account platform. The official version 1.0.0 is now released. If you encounter problems using this SDK, please contact me. If you think this project is helpful to you, please give it a star and let me know. Thank you.
composer require chunhei2008/easy-open-wechat
github wiki
Except for the related configuration fields of the newly added third-party platform, the other fields are still the configuration fields of EasyWechat.
$config = [
'debug' => false, //是否调试模式
'component_app_id' => 'component app id', //第三方公众平台app id
'component_app_secret' => 'component app secret', //第三方公众平台app secret
'token' => 'token', //公众号消息校验Token
'aes_key' => 'aea key', //公众号消息加解密Key
'redirect_uri' => 'auth callback uri', //授权回调页面URI
'log' => [ //日志
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
Third-party platforms can place a "WeChat Official Account Authorization" entrance on their websites to guide public account operators to the authorization page. The authorization page URL is https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx. In this URL, the third-party platform party needs to provide the third-party platform party appid, pre-authorization code and callback. URI
<?php
/**
* auth.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:18
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
$page = $app->login->getLoginPage();
echo "<a href="$page">auth</a>";
After the authorization process is completed, the authorization page will automatically jump to the callback URI, and return the authorization code and expiration time in the URL parameters (redirect_url?auth_code=xxx&expires_in=600); after obtaining the authorization code, the third-party platform can use the authorization code In exchange for the interface call credentials of the authorized official account (authorizer_a ccess_token, also referred to as token), then call the credentials through this interface, and follow the instructions in the official account developer documentation (mp.weixin.qq.com/wiki) to call the official account related API (which APIs can be called depends on Which set of permissions the user authorizes to the third-party platform also depends on which interface permissions the official account itself has), using JS SDK and other capabilities. For details, please see [Interface Description of Official Account Third-Party Platform]
<?php
/**
* authcallback.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:55
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
// 回调页面带回的授权码
$auth_code = $_GET['auth_code'];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
// 使用授权码获取授权公众号的信息,并且自动保存公众号的refresh_token等
$auth_info = $app->authorization->setAuthorizationCode($auth_code)->getAuthorizationInfo();
var_dump($auth_info);
Response to all authorization events in the authorization process, including network-wide release monitoring response
<?php
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
$app->auth->handle()->send();
<?php
/**
* message.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:13
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
'debug' => true,
'component_app_id' => '第三方平台app id',
'component_app_secret' => '第三方平台app secret',
'token' => '公众号消息校验Token
',
'aes_key' => '公众号消息加解密Key
',
'redirect_uri' => '授权回调页面url',
'log' => [
'level' => 'debug',
'file' => '/tmp/easyopenwechat.log',
],
];
//公众号消息与事件接收URL中带的$APPID$
$app_id = $_GET['app_id'];
$config['app_id'] = $app_id;
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
//获取easywechat的app对象
$wechat = $app->wechat;
//余下的和EasyWechat开发一模一样
$response = $wechat->server->setMessageHandler(function ($message) {
return "您好!欢迎关注我! this is easy open wechat";
})->serve();
$response->send();
The SDK uses Cache by default to cache and store key information of public accounts. However, key information data storage such as authorizer_appid, authorizer_refresh_token, etc. is best stored in a persistent storage place such as a database. This SDK also takes this aspect into consideration.
The default implementation uses Cache for storage
<?php
/**
* AuthorizerRefreshToken.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 11:44
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatCore;
use Chunhei2008EasyOpenWechatContractsAuthorizerRefreshTokenContract;
use Chunhei2008EasyOpenWechatSupportLog;
use Chunhei2008EasyOpenWechatTraitsCacheTrait;
use DoctrineCommonCacheCache;
class AuthorizerRefreshToken implements AuthorizerRefreshTokenContract
{
use CacheTrait;
/**
* app id
*
* @var string
*/
protected $authorizerAppId = '';
/**
* cache key prefix
*/
const AUTHORIZER_REFRESH_TOKEN_CACHE_PREFIX = 'easyopenwechat.core.refresh_token.';
public function __construct(Cache $cache = null)
{
$this->cache = $cache;
$this->setCacheKeyField('authorizerAppId');
$this->setPrefix(static::AUTHORIZER_REFRESH_TOKEN_CACHE_PREFIX);
}
/**
*
* get refresh token
*
* @param $authorizerAppId
*
* @return mixed|string
*/
public function getRefreshToken($authorizerAppId)
{
$this->setAuthorizerAppId($authorizerAppId);
$cacheKey = $this->getCacheKey();
$authorizerRefreshToken = $this->getCache()->fetch($cacheKey);
Log::debug('Get refresh token from cache:', [$authorizerAppId, $authorizerRefreshToken]);
return $authorizerRefreshToken;
}
/**
* set refresh token
*
* @param $authorizerAppId
* @param $authorizerRefreshToken
*/
public function setRefreshToken($authorizerAppId, $authorizerRefreshToken)
{
$this->setAuthorizerAppId($authorizerAppId);
$cacheKey = $this->getCacheKey();
$this->getCache()->save($cacheKey, $authorizerRefreshToken);
Log::debug('Set refresh token:', [$authorizerAppId, $authorizerRefreshToken]);
}
/**
*
* remove refresh token
*
* @param $authorizerAppId
*/
public function removeRefreshToken($authorizerAppId)
{
$this->setAuthorizerAppId($authorizerAppId);
$cacheKey = $this->getCacheKey();
$this->getCache()->delete($cacheKey);
Log::debug('Remove refresh token:', [$authorizerAppId]);
}
/**
* set authorizer app id
*
* @param $authorizerAppId
*/
private function setAuthorizerAppId($authorizerAppId)
{
$this->authorizerAppId = $authorizerAppId;
}
}
<?php
/**
* AuthorizerRefreshToken.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 11:44
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatCore;
use Chunhei2008EasyOpenWechatContractsAuthorizerRefreshTokenContract;
use Chunhei2008EasyOpenWechatSupportLog;
use Chunhei2008EasyOpenWechatTraitsCacheTrait;
use DoctrineCommonCacheCache;
class AuthorizerRefreshTokenDB implements AuthorizerRefreshTokenContract
{
/**
*
* get refresh token
*
* @param $authorizerAppId
*
* @return mixed|string
*/
public function getRefreshToken($authorizerAppId)
{
// get refresh token by app id from db
Log::debug('Get refresh token from cache:', [$authorizerAppId, $authorizerRefreshToken]);
return $authorizerRefreshToken;
}
/**
* set refresh token
*
* @param $authorizerAppId
* @param $authorizerRefreshToken
*/
public function setRefreshToken($authorizerAppId, $authorizerRefreshToken)
{
// save refresh token to db by app id
Log::debug('Set refresh token:', [$authorizerAppId, $authorizerRefreshToken]);
}
/**
*
* remove refresh token
*
* @param $authorizerAppId
*/
public function removeRefreshToken($authorizerAppId)
{
// delete refresh token from db by app id
Log::debug('Remove refresh token:', [$authorizerAppId]);
}
}
<?php
/**
* AuthorizerRefreshTokenDefaultProvider.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 11:50
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatFoundationServiceProviders;
use Chunhei2008EasyOpenWechatCoreAuthorizerRefreshTokenDB;
use PimpleContainer;
use PimpleServiceProviderInterface;
class AuthorizerRefreshTokenDBProvider implements ServiceProviderInterface
{
public function register(Container $pimple)
{
//覆盖refresh token
$pimple['authorizer_refresh_token'] = function ($pimple) {
return new AuthorizerRefreshTokenDB(
$pimple['db']
);
};
}
}
<?php
/**
* message.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:13
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
$providers = [
AuthorizerRefreshTokenDBProvider::class
];
$app->addProviders($providers);
Handle responses to authorization events and process related data after authorization
<?php
namespace Chunhei2008EasyOpenWechatContracts;
use Chunhei2008EasyOpenWechatCoreAuthorization;
use Chunhei2008EasyOpenWechatCoreAuthorizationInfo;
use Chunhei2008EasyOpenWechatCoreComponentVerifyTicket;
/**
* AuthPushContract.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/16 09:25
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
interface AuthorizeHandlerContract
{
/**
* handle component verify ticket
*
* @param $message
* @param ComponentVerifyTicket $componentVerifyTicket
*
* @return mixed
*/
public function componentVerifyTicket($message, ComponentVerifyTicket $componentVerifyTicket);
/**
* handle authorized
*
* @param $message
* @param AuthorizationInfo $authorizationInfo
*
* @return mixed
*/
public function authorized($message, Authorization $authorization);
/**
* handle unauthorized
*
* @param $message
*
* @return mixed
*/
public function unauthorized($message, AuthorizerRefreshTokenContract $authorizerRefreshToken);
/**
*
* handle updateauthorized
*
* @param $message
* @param AuthorizationInfo $authorizationInfo
*
* @return mixed
*/
public function updateauthorized($message , Authorization $authorization);
}
<?php
/**
* AuthorizeHandlerServiceProvider.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/17 16:34
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
namespace Chunhei2008EasyOpenWechatFoundationServiceProviders;
use Chunhei2008EasyOpenWechatCoreAuthorizeHandler;
use PimpleContainer;
use PimpleServiceProviderInterface;
class AuthorizeHandlerCustomerServiceProvider implements ServiceProviderInterface
{
public function register(Container $pimple)
{
//覆盖
$pimple['authorize_handler'] = function ($pimple) {
return new AuthorizeHandlerCustomer();
};
}
}
<?php
/**
* message.php
*
* Author: wangyi <[email protected]>
*
* Date: 2016/12/18 09:13
* Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
*/
include "./vendor/autoload.php";
$config = [
];
$app = new Chunhei2008EasyOpenWechatFoundationApplication($config);
//添加到容器
$providers = [
AuthorizeHandlerCustomerServiceProvider::class
];
$app->addProviders($providers);