instructions

This SDK is developed based on EasyWechat. In order to facilitate the development of third-party platform of wechat public account, it encapsulates the development of third-party platform authorized by wechat public account and realizes the access of whole network release. This project is still under continuous development. Relevant functions are still being improved. If you have any questions, please contact me. If you think this project is helpful to you, please give me a STAR

configuration

Except the newly added related configuration fields of the third-party platform, other fields are still the configuration fields of EasyWechat

$config = ['component_app_id' => 'Component app ID ', $config = ['component_app_id' =>' Component app ID ', Component_app_secret '=> 'component_app_secret', // component_app_secret' => 'token', // Public Token Token 'aes_key' => 'aea key', // public Token Token 'redirect_uri' => 'auth callback URI ', / / callback authorization page URI 'log' = > [/ / log 'level' = > 'debug', 'file' = > '/ TMP/easyopenwechat. Log,],];Copy the code

[email protected]> * * Date: 2016/12/18 09:18 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd. */ include "./vendor/autoload.php"; $config = ['component_app_id' => true, 'component_app_id' => 'component_app_id', 'component_app_secret' => 'component_app_id', 'token' => 'public number message verification token',' AES_key '=>' public number message encryption Key ', 'redirect_URI' => 'Authorization callback page URL ', 'log' => ['level' => 'debug', 'file' => '/tmp/easyopenwechat.log', ], ]; $app = new \Chunhei2008\EasyOpenWechat\Foundation\Application($config); $page = $app->login->getLoginPage(); echo "<a href=\"$page\">auth</a>";Copy the code

Authorization callback page

After the authorization process is complete, the authorization page automatically switches to the callback URI and returns the authorization code and expiration time (redirect_URL? auth_code=xxx&expires_in=600); After obtaining the authorization code, the third party platform can use the authorization code to exchange the interface call credential (authorizer_access_token, also referred to as token) of the authorized public number, and then invoke the credential through the interface. According to the instructions of the developer document of the public number (mp.weixin.qq.com/wiki), To call the relevant API of the public account (which API can be called depends on which permission set the user has authorized to the third party platform, and also depends on the interface permissions of the public account itself), use JS SDK and other capabilities. For details, please refer to [Interface Description of third-party platform of public Account].

<? 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 = ['component_app_id' => true, 'component_app_id' => 'component_app_id', 'component_app_secret' => 'component_app_id', 'token' => 'public number message verification token',' AES_key '=>' public number message encryption Key ', 'redirect_URI' => 'Authorization callback page URL ', 'log' => ['level' => 'debug', 'file' => '/tmp/easyopenwechat.log', ], ]; $auth_code = $_GET['auth_code']; $app = new \Chunhei2008\EasyOpenWechat\Foundation\Application($config); // Use the authorization code to obtain the information of the authorized public number, $auth_info = $app->authorization->setAuthorizationCode($auth_code)->getAuthorizationInfo($auth_code)->getAuthorizationInfo(); var_dump($auth_info);Copy the code

Public account message and event handling

<? 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 = ['component_app_id' => true, 'component_app_id' => 'component_app_id', 'component_app_secret' => 'component_app_id', 'token' => 'public number message verification token',' AES_key '=>' public number message encryption Key ', 'redirect_URI' => 'Authorization callback page URL ', 'log' => ['level' => 'debug', 'file' => '/tmp/easyopenwechat.log', ], ]; $app_id = $_GET['app_id']; $app_id = $_GET['app_id']; $config['app_id'] = $app_id; $app = new \Chunhei2008\EasyOpenWechat\Foundation\Application($config); $easywechat = $app->wechat; $response = $wechat->server->setMessageHandler(function ($message) {return "hello! Welcome to follow me! this is easy open wechat"; })->serve(); $response->send();Copy the code

Authorizer_refresh_token store and obtain

[email protected]>
 *
 * Date:   2016/12/17 11:44
 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
 */

namespace Chunhei2008\EasyOpenWechat\Core;

use Chunhei2008\EasyOpenWechat\Contracts\AuthorizerRefreshTokenContract;
use Chunhei2008\EasyOpenWechat\Support\Log;
use Chunhei2008\EasyOpenWechat\Traits\CacheTrait;
use Doctrine\Common\Cache\Cache;

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;
    }

}
Copy the code

The custom

  1. The database
<? php /** * AuthorizerRefreshToken.php * * Author: wangyi <[email protected]> * * Date: 2016/12/17 11:44 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd. */ namespace Chunhei2008\EasyOpenWechat\Core; use Chunhei2008\EasyOpenWechat\Contracts\AuthorizerRefreshTokenContract; use Chunhei2008\EasyOpenWechat\Support\Log; use Chunhei2008\EasyOpenWechat\Traits\CacheTrait; use Doctrine\Common\Cache\Cache; 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]); }}Copy the code
  1. Service provider
<? php /** * AuthorizerRefreshTokenDefaultProvider.php * * Author: wangyi <[email protected]> * * Date: 2016/12/17 11:50 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd. */ namespace Chunhei2008\EasyOpenWechat\Foundation\ServiceProviders; use Chunhei2008\EasyOpenWechat\Core\AuthorizerRefreshTokenDB; use Pimple\Container; use Pimple\ServiceProviderInterface; class AuthorizerRefreshTokenDBProvider implements ServiceProviderInterface { public function register(Container $pimple) $pimple['authorizer_refresh_token'] = function ($pimple) {return new AuthorizerRefreshTokenDB($pimple) $pimple['db'] ); }; }}Copy the code
  1. The service provider is bound to the container
<? 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 \Chunhei2008\EasyOpenWechat\Foundation\Application($config); $providers = [ AuthorizerRefreshTokenDBProvider::class ]; $app->addProviders($providers);Copy the code

[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); }Copy the code
  1. The service provider is bound to the container
<? php /** * AuthorizeHandlerServiceProvider.php * * Author: wangyi <[email protected]> * * Date: 2016/12/17 16:34 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd. */ namespace Chunhei2008\EasyOpenWechat\Foundation\ServiceProviders; use Chunhei2008\EasyOpenWechat\Core\AuthorizeHandler; use Pimple\Container; use Pimple\ServiceProviderInterface; class AuthorizeHandlerCustomerServiceProvider implements ServiceProviderInterface { public function register(Container $pimple[' authorize_Handler '] = function ($pimple) {return new AuthorizeHandlerCustomer(); }; }}Copy the code
  1. The service provider is bound to the container
<? 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 \Chunhei2008\EasyOpenWechat\Foundation\Application($config); / / added to the container $will = [AuthorizeHandlerCustomerServiceProvider: : class]; $app->addProviders($providers);Copy the code

Thank you

  1. EasyWeChat