说明:此示例为爱程序网 www.iiexe.com 原创发布!
内容包含:可以正常运行的代码包
网络上出现可各种插件及管理平台!但是我觉得最好 还是自己开发一个比较好下面是代码!可以直接运行
下面是类库
最下面是 示例代码
<?php /** * 微信公众平台 PHP SDK ---- 爱程序 * * @author NetPuter <netputer@gmail.com>//这个是官方发布者邮箱不是爱程序网的 */ require('Wechat.php');//引入类库 /*文件下载地址 改名为PHP文件 http://www.iiexe.com/weixin/example/Wechat.txt*/ /*************************************************************************/ /*开发者信息 在开发模式下申请开发者以后 或者公众平台服务号认证后可获取*/ $APPID="wx4080bcfb1b5d1605"; $APPSECRET="053d0cb15accfde456778b7f073db3e3"; /*获取ACC_TOKEN值作为创建微信菜单使用*/ $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET; $json=file_get_contents($TOKEN_URL); $result=json_decode($json,true); $ACC_TOKEN=$result['access_token']; echo $ACC_TOKEN;//默认输出 可以注释到输出到也没的$ACC_TOKEN值 /*创建菜单开始===创建菜单josn数据*/ $data = ' { "button":[ { "name":"公共查询", "sub_button":[ { "type":"click", "name":"k552查询", "key":"K552" }, { "type":"click", "name":"k554查询", "key":"K554" }, { "type":"click", "name":"二维码", "key":"wkey" }] }, { "name":"爱程序网", "sub_button":[ { "type":"click", "name":"爱上爱程序网", "key":"Exploit1" }, { "type":"click", "name":"爱程序网景点", "key":"Exploit2" }, { "type":"click", "name":"爱程序网美食", "key":"Exploit3" }, { "type":"click", "name":"爱程序网设计", "key":"Exploit4" }] }, { "type":"click", "name":"联系我们", "key":"lianxiUs" }] }'; /**创建菜单======post提交josn数据到腾讯服务器添加菜单*/ $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACC_TOKEN; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $MENU_URL); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $info = curl_exec($ch); if (curl_errno($ch)) { echo 'Errno'.curl_error($ch); } curl_close($ch); //var_dump($info);//显示信息 默认注释 /***************************************************************************/ /** * 微信公众平台演示类 */ class MyWechat extends Wechat { /** * 用户关注时触发,回复「欢迎关注」 * 关注是回复的是图文消息 参数 ,标题,标题,图片,URL地址 * @return void */ protected function onSubscribe() { $items = array( new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), ); $this->responseNews($items); } /** * 用户已关注时,扫描带参数二维码时触发,回复二维码的EventKey (测试帐号似乎不能触发) * * @return void */ protected function onScan() { $this->responseText('二维码的EventKey:' . $this->getRequest('EventKey')); } /** * 用户取消关注时触发 * * @return void */ protected function onUnsubscribe() { // 「悄悄的我走了,正如我悄悄的来;我挥一挥衣袖,不带走一片云彩。」 } /** * 上报地理位置时触发,回复收到的地理位置 * * @return void */ protected function onEventLocation() { $this->responseText('收到了位置推送:' . $this->getRequest('Latitude') . ',' . $this->getRequest('Longitude')); } /** * 收到文本消息时触发,回复收到的文本消息内容 * * @return void */ protected function onText() { $ses = $this->getRequest('content'); if($ses == "help"){ $this->responseText('帮助信息:' . $this->getRequest('content')); } $this->responseText('收到了文字消息2:' . $this->getRequest('content')); } /** * 收到图片消息时触发,回复由收到的图片组成的图文消息 * * @return void */ protected function onImage() { $items = array( new NewsResponseItem('标题一', '描述一', $this->getRequest('picurl'), $this->getRequest('picurl')), new NewsResponseItem('标题二', '描述二', $this->getRequest('picurl'), $this->getRequest('picurl')), ); $this->responseNews($items); } /** * 收到地理位置消息时触发,回复收到的地理位置 * * @return void */ protected function onLocation() { $num = 1 / 0; // 故意触发错误,用于演示调试功能 $this->responseText('收到了位置消息:' . $this->getRequest('location_x') . ',' . $this->getRequest('location_y')); } /** * 收到链接消息时触发,回复收到的链接地址 * * @return void */ protected function onLink() { $this->responseText('收到了链接:' . $this->getRequest('url')); } /** * 收到语音消息时触发,回复语音识别结果(需要开通语音识别功能) * * @return void */ protected function onVoice() { $this->responseText('收到了语音消息,识别结果为:' . $this->getRequest('Recognition')); } /** * 收到自定义菜单消息时触发,回复菜单的EventKey * 回复内容KEY为累加值 可以添加判断返回数据 * @return void */ protected function onClick() { $key1 = $this->getRequest('EventKey'); if($key1 == "lianxiUs"){ $this->responseText('地址:中国.牡丹江|QQ:369488963|网站:http://www.iiexe.com '); } $key2 = $this->getRequest('EventKey'); if($key2 == "K554"){ $items = array( new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://checi.tieyou.com/K554.html'), ); $this->responseNews($items); } $key3 = $this->getRequest('EventKey'); if($key3 == "K552"){ $items = array( new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://checi.tieyou.com/K552.html'), ); $this->responseNews($items); } $key4 = $this->getRequest('EventKey'); if($key4 == "wkey"){ $items = array( new NewsResponseItem('二维码', '扫描二维码','http://www.iiexe.com/weixin/example/00.jpg','http://www.iiexe.com/weixin/example/00.jpg'), ); $this->responseNews($items); } $items = array( new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), ); $this->responseNews($items); } /**************************************************************************************/ /** * 收到未知类型消息时触发,回复收到的消息类型 * * @return void */ protected function onUnknown() { $this->responseText('收到了未知类型消息:' . $this->getRequest('msgtype')); } } $wechat = new MyWechat('exploit', FALSE); $wechat->run();
<?php /** * 微信公众平台 PHP SDK ---- 爱程序 * * @author NetPuter <netputer@gmail.com>//这个是官方发布者邮箱不是爱程序网的 */ require('Wechat.php');//引入类库 /*文件下载地址 改名为PHP文件 http://www.iiexe.com/weixin/example/Wechat.txt*/ /*************************************************************************/ /*开发者信息 在开发模式下申请开发者以后 或者公众平台服务号认证后可获取*/ $APPID="wx4080bcfb1b5d1605"; $APPSECRET="053d0cb15accfde456778b7f073db3e3"; /*获取ACC_TOKEN值作为创建微信菜单使用*/ $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET; $json=file_get_contents($TOKEN_URL); $result=json_decode($json,true); $ACC_TOKEN=$result['access_token']; echo $ACC_TOKEN;//默认输出 可以注释到输出到也没的$ACC_TOKEN值 /*创建菜单开始===创建菜单josn数据*/ $data = ' { "button":[ { "name":"公共查询", "sub_button":[ { "type":"click", "name":"k552查询", "key":"K552" }, { "type":"click", "name":"k554查询", "key":"K554" }, { "type":"click", "name":"二维码", "key":"wkey" }] }, { "name":"爱程序网", "sub_button":[ { "type":"click", "name":"爱上爱程序网", "key":"Exploit1" }, { "type":"click", "name":"爱程序网景点", "key":"Exploit2" }, { "type":"click", "name":"爱程序网美食", "key":"Exploit3" }, { "type":"click", "name":"爱程序网设计", "key":"Exploit4" }] }, { "type":"click", "name":"联系我们", "key":"lianxiUs" }] }'; /**创建菜单======post提交josn数据到腾讯服务器添加菜单*/ $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$ACC_TOKEN; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $MENU_URL); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $info = curl_exec($ch); if (curl_errno($ch)) { echo 'Errno'.curl_error($ch); } curl_close($ch); //var_dump($info);//显示信息 默认注释 /***************************************************************************/ /** * 微信公众平台演示类 */ class MyWechat extends Wechat { /** * 用户关注时触发,回复「欢迎关注」 * 关注是回复的是图文消息 参数 ,标题,标题,图片,URL地址 * @return void */ protected function onSubscribe() { $items = array( new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), new NewsResponseItem('标题一', '描述一', 'http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com'), ); $this->responseNews($items); } /** * 用户已关注时,扫描带参数二维码时触发,回复二维码的EventKey (测试帐号似乎不能触发) * * @return void */ protected function onScan() { $this->responseText('二维码的EventKey:' . $this->getRequest('EventKey')); } /** * 用户取消关注时触发 * * @return void */ protected function onUnsubscribe() { // 「悄悄的我走了,正如我悄悄的来;我挥一挥衣袖,不带走一片云彩。」 } /** * 上报地理位置时触发,回复收到的地理位置 * * @return void */ protected function onEventLocation() { $this->responseText('收到了位置推送:' . $this->getRequest('Latitude') . ',' . $this->getRequest('Longitude')); } /** * 收到文本消息时触发,回复收到的文本消息内容 * * @return void */ protected function onText() { $ses = $this->getRequest('content'); if($ses == "help"){ $this->responseText('帮助信息:' . $this->getRequest('content')); } $this->responseText('收到了文字消息2:' . $this->getRequest('content')); } /** * 收到图片消息时触发,回复由收到的图片组成的图文消息 * * @return void */ protected function onImage() { $items = array( new NewsResponseItem('标题一', '描述一', $this->getRequest('picurl'), $this->getRequest('picurl')), new NewsResponseItem('标题二', '描述二', $this->getRequest('picurl'), $this->getRequest('picurl')), ); $this->responseNews($items); } /** * 收到地理位置消息时触发,回复收到的地理位置 * * @return void */ protected function onLocation() { $num = 1 / 0; // 故意触发错误,用于演示调试功能 $this->responseText('收到了位置消息:' . $this->getRequest('location_x') . ',' . $this->getRequest('location_y')); } /** * 收到链接消息时触发,回复收到的链接地址 * * @return void */ protected function onLink() { $this->responseText('收到了链接:' . $this->getRequest('url')); } /** * 收到语音消息时触发,回复语音识别结果(需要开通语音识别功能) * * @return void */ protected function onVoice() { $this->responseText('收到了语音消息,识别结果为:' . $this->getRequest('Recognition')); } /** * 收到自定义菜单消息时触发,回复菜单的EventKey * 回复内容KEY为累加值 可以添加判断返回数据 * @return void */ protected function onClick() { $key1 = $this->getRequest('EventKey'); if($key1 == "lianxiUs"){ $this->responseText('地址:中国.牡丹江|QQ:369488963|网站:http://www.iiexe.com '); } $key2 = $this->getRequest('EventKey'); if($key2 == "K554"){ $items = array( new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://checi.tieyou.com/K554.html'), ); $this->responseNews($items); } $key3 = $this->getRequest('EventKey'); if($key3 == "K552"){ $items = array( new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://checi.tieyou.com/K552.html'), ); $this->responseNews($items); } $key4 = $this->getRequest('EventKey'); if($key4 == "wkey"){ $items = array( new NewsResponseItem('二维码', '扫描二维码','http://www.iiexe.com/weixin/example/00.jpg','http://www.iiexe.com/weixin/example/00.jpg'), ); $this->responseNews($items); } $items = array( new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), new NewsResponseItem('标题一', '描述一','http://www.iiexe.com/weixin/example/lc.png','http://www.iiexe.com/'), ); $this->responseNews($items); } /**************************************************************************************/ /** * 收到未知类型消息时触发,回复收到的消息类型 * * @return void */ protected function onUnknown() { $this->responseText('收到了未知类型消息:' . $this->getRequest('msgtype')); } } $wechat = new MyWechat('exploit', FALSE); $wechat->run();