PHP code example of zhouzeken / wxsdk
1. Go to this page and download the library: Download zhouzeken/wxsdk 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/ */
zhouzeken / wxsdk example snippets regexp
$config = [
'appid' => '',
'secret' => '',
'grant_type' => 'client_credential'
];
$res = \zhouzeken\wxsdk\Init::getInstance($config)->AccessToken()->getAccessToken();
regexp
$config = [
'appid' => '',
'secret' => '',
'grant_type' => 'client_credential',
'force_refresh' => false
];
$res = \zhouzeken\wxsdk\Init::getInstance($config)->AccessToken()->getStableAccessToken();
regexp
$config = [
'appid' => '',
'secret' => '',
];
$res = \zhouzeken\wxsdk\Init::getInstance($config)->menu()->getMenu(['access_token'=>'传入Access Token']);
regexp
$config = [
'appid' => '',
'secret' => '',
];
//button格式和官方一致
$params = [
'access_token'=>'传入Access Token',
'button' => [
['name'=>'菜单1','sub_button'=>[
['type'=>'click','name'=>'点击','key'=>'key_111'],
['type'=>'view','name'=>'跳转','url'=>'http://www.baidu.com/'],
['type'=>'miniprogram','name'=>'小程序','url'=>'http://www.baidu.com/','appid'=>'wx9c1bffcf8ad42fe0','pagepath'=>'pages/homepage/homepage'],
['type'=>'scancode_push','name'=>'扫码推事件','key'=>'key_222','sub_button'=>[]],
['type'=>'scancode_waitmsg','name'=>'扫码带提示','key'=>'key_333','sub_button'=>[]],
]],
['name'=>'菜单2','sub_button'=>[
['type'=>'pic_sysphoto','name'=>'系统拍照发图','key'=>'key_444','sub_button'=>[]],
]]
]
];
$res = \zhouzeken\wxsdk\Init::getInstance($config)->menu()->createMenu($params);
regexp
$config = [
'appid' => '',
'secret' => '',
];
$params = [
'access_token'=>'传入Access Token',
];
$res = \zhouzeken\wxsdk\Init::getInstance($config)->menu()->deleteMenu($params);
regexp
$token = ''; //对应公众号配置的令牌(Token)
$EncodingAESKey = ''; //对应公众号配置的消息加解密密钥(EncodingAESKey)
#接收事件推送的自动回复配置config(具体回复模板在下面)
$push_config = [
//用户关注公众号
'event.subscribe' => [
#下面都是属于回复,具体看下面回复模板
'msgTpl' => 'text',
'Content' => '感谢关注公众号'
],
//用户取消关注公众号
'event.unsubscribe' => [
'msgTpl' => 'text',
'Content' => '很遗憾您取消关注了,我们下回再见'
],
//点击菜单跳转链接时的事件推送
'event.VIEW' => [
'msgTpl' => 'text',
'Content' => '即将跳转。。。'
],
//点击事件推送(示例1),key_111对应设置的菜单key
'event.CLICK.key_111' => [
'msgTpl' => 'text',
'Content' => '你触发了设置的菜单栏目1。。。'
],
]
#接收用户发送普通消息到公众号的配置config
$text_config = [
]
\zhouzeken\wxsdk\Init::getInstance([
'token' => $token,
'EncodingAESKey' => $EncodingAESKey
])->receive()->start($push_config,$text_config);
regexp
[
'msgTpl' => 'text',
'Content' => '感谢关注公众号啊老铁。。。'
]
regexp
[
'msgTpl' => 'image',
'MediaId' => '图片素材ID'
]
regexp
[
'msgTpl' => 'voice',
'MediaId' => '语音素材ID'
]
regexp
[
'msgTpl' => 'video',
'MediaId' => '视频素材ID',
'Title' => '视频标题',
'Description' => '视频描述'
]
regexp
[
'msgTpl' => 'music',
'Title' => '音乐标题',
'Description' => '音乐链接',
'MusicUrl' => '高质量音乐链接,WIFI环境优先使用该链接播放音乐',
'ThumbMediaId' => '缩略图的媒体id,通过素材管理中的接口上传多媒体文件,得到的id'
]
regexp
[
'msgTpl' => 'news',
'Content' => '感谢关注公众号啊老铁。。。'
]