PHP code example of chunhei2008 / easy-open-wechat

1. Go to this page and download the library: Download chunhei2008/easy-open-wechat library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chunhei2008 / easy-open-wechat example snippets



/**
 * auth.php
 *
 * Author: wangyi <[email protected]>
 *
 * Date:   2016/12/18 09:18
 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
 */

ken'                => '公众号消息校验Token
',
    'aes_key'              => '公众号消息加解密Key

',
    'redirect_uri' => '授权回调页面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>";


/**
 * authcallback.php
 *
 * Author: wangyi <[email protected]>
 *
 * Date:   2016/12/18 09:55
 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
 */
             => '公众号消息校验Token
',
    'aes_key'              => '公众号消息加解密Key

',
    'redirect_uri' => '授权回调页面url',
    'log' => [
        'level' => 'debug',
        'file'  => '/tmp/easyopenwechat.log',
    ],
];

// 回调页面带回的授权码
$auth_code = $_GET['auth_code'];   

$app = new \Chunhei2008\EasyOpenWechat\Foundation\Application($config);

// 使用授权码获取授权公众号的信息,并且自动保存公众号的refresh_token等
$auth_info = $app->authorization->setAuthorizationCode($auth_code)->getAuthorizationInfo();


var_dump($auth_info);



ig = [
    '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 \Chunhei2008\EasyOpenWechat\Foundation\Application($config);

$app->auth->handle()->send();


/**
 * message.php
 *
 * Author: wangyi <[email protected]>
 *
 * Date:   2016/12/18 09:13
 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
 */

'                => '公众号消息校验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 \Chunhei2008\EasyOpenWechat\Foundation\Application($config);

//获取easywechat的app对象
$wechat = $app->wechat;

//余下的和EasyWechat开发一模一样
$response = $wechat->server->setMessageHandler(function ($message) {
    return "您好!欢迎关注我! this is easy open wechat";
})->serve();

$response->send();


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

}


/**
 * 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)
    {
        //覆盖refresh token
        $pimple['authorizer_refresh_token'] = function ($pimple) {
            return new AuthorizerRefreshTokenDB(
                $pimple['db']
            );
        };
    }
}


/**
 * message.php
 *
 * Author: wangyi <[email protected]>
 *
 * Date:   2016/12/18 09:13
 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
 */

ers($providers);



namespace Chunhei2008\EasyOpenWechat\Contracts;

use Chunhei2008\EasyOpenWechat\Core\Authorization;
use Chunhei2008\EasyOpenWechat\Core\AuthorizationInfo;
use Chunhei2008\EasyOpenWechat\Core\ComponentVerifyTicket;


/**
 * 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);

}



/**
 * 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)
    {
        //覆盖
        $pimple['authorize_handler'] = function ($pimple) {
            return new AuthorizeHandlerCustomer();
        };
    }

}


/**
 * message.php
 *
 * Author: wangyi <[email protected]>
 *
 * Date:   2016/12/18 09:13
 * Copyright: (C) 2014, Guangzhou YIDEJIA Network Technology Co., Ltd.
 */

class
];

$app->addProviders($providers);