PHP code example of pfinal / wechat

1. Go to this page and download the library: Download pfinal/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/ */

    

pfinal / wechat example snippets




Final\Wechat\Kernel;
use PFinal\Wechat\Message\Receive;
use PFinal\Wechat\Message;
use PFinal\Wechat\WechatEvent;
use PFinal\Wechat\Support\Log;

//配置项 
$config = array(
    'appId' => 'xxxxxxxxx',
    'appSecret' => 'xxxxxxxxxxxxxxxxxxxx',
    'token' => 'xxxxxx',
    'encodingAesKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    //更多详细配置请参考 demo/config-local.example
);

//初始化
Kernel::init($config);

//消息处理
Kernel::register(Receive::TYPE_TEXT, function (WechatEvent $event) {
    $message = $event->getMessage();
    $event->setResponse('你好');
    $event->stopPropagation();
});

//关注事件
Kernel::register(Receive::TYPE_EVENT_SUBSCRIBE, function (WechatEvent $event) {
    $event->setResponse('你关注或是不关注,我都在这里,不悲不喜~~');
    $event->stopPropagation();
});

//处理微信服务器的请求
$response = Kernel::handle();

echo $response;



putenv('WECHAT_PROXY', '127.0.0.1');
putenv('WECHAT_PROXYPORT', '8080');

# proxy server
# https://github.com/pfinal/proxy
# curl -o proxy https://github.com/pfinal/proxy/releases/download/v1.0.0/proxy-linux
# chmod +x proxy
# ./proxy --port :8080

putenv('WECHAT_ACCESS_TOKEN_SERVER', 'http://192.168.1.33/wechat-access-token');

中控服务器接收参数:
$_POST['appId']
$_POST['useCache']  '1'表示可用缓存  '0' 表示不用缓存
响应内容 {"status": true, "access_token": "xxx"}