PHP code example of randy / weopen-developer
1. Go to this page and download the library: Download randy/weopen-developer 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/ */
randy / weopen-developer example snippets
// 配置参数(可以公众号服务平台获取)
$config = [
'component_appid' => 'wx4e63e993e222df8d',
'component_token' => 'P8QHTIxpBEq88IrxatqhgpBm2OAQROkI',
'component_appsecret' => '7cfa1afa87a41e2ea3445cea015c0974',
'component_encodingaeskey' => 'L5uFIa0U6KLalPyXckyqoVIJYLhsfrg8k9YzybZIHsx',
];
// 注册授权公众号 AccessToken 处理
$config['GetAccessTokenCallback'] = function ($authorizer_appid) use ($config) {
$open = new \WeOpen\Service($config);
$authorizer_refresh_token = ''; // 通过$authorizer_appid从数据库去找吧,在授权绑定的时候获取
$result = $open->refreshAccessToken($authorizer_appid, $authorizer_refresh_token);
if (empty($result['authorizer_access_token'])) {
throw new \WeChat\Exceptions\InvalidResponseException($result['errmsg'], '0');
}
$data = [
'authorizer_access_token' => $result['authorizer_access_token'],
'authorizer_refresh_token' => $result['authorizer_refresh_token'],
];
// 需要把$data记录到数据库
return $result['authorizer_access_token'];
};
try{
// 实例公众号服务接口
$server = new \WeOpen\Service($config);
// 获取并更新Ticket推送
if (!($data = $server->getComonentTicket())) {
return "Ticket event handling failed.";
}
} catch (Exception $e) {
// 出错啦,处理下吧
echo $e->getMessage() . PHP_EOL;
}