接了个小活:要求公众号中用户回复1,公众号被动回复7位数验证码,同时把用户openId和验证码写入数据库
简单写了各测试demo:工具 thinkPHP 订阅号子钦有话说内申请的测试账号
接口配置信息修改
}
</span><span style="color: #008000">/*</span><span style="color: #008000">public function show(){
echo 'zhangmangde';
}</span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000">接受事件,推送并回复</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> responseMsg(){
</span><span style="color: #008000">//</span><span style="color: #008000">1.获取到微信推送过来的post数据(xml格式)</span>
<span style="color: #800080">$postArr</span> = <span style="color: #800080">$GLOBALS</span>['HTTP_RAW_POST_DATA'<span style="color: #000000">];
</span><span style="color: #008000">//</span><span style="color: #008000">2.处理消息类型,并设置回复内容和类型</span>
<span style="color: #008000">/*</span><span style="color: #008000"><xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml></span><span style="color: #008000">*/</span>
<span style="color: #800080">$postObj</span> = <span style="color: #008080">simplexml_load_string</span>(<span style="color: #800080">$postArr</span>);<span style="color: #008000">//</span><span style="color: #008000">将xml字符串载入对象中
//$postObj->ToUserName = '';
//$postObj->FromUserName = '';
//$postObj->CreateTime = '';
//$postObj->MsgType = '';
//$postObj->Event = '';
//判断该数据包是否是订阅的事件推送</span>
<span style="color: #0000ff">if</span>(<span style="color: #008080">strtolower</span>(<span style="color: #800080">$postObj</span>->MsgType) == 'event'<span style="color: #000000">){
</span><span style="color: #008000">//</span><span style="color: #008000">如果是关注subcribe事件</span>
<span style="color: #0000ff">if</span>(<span style="color: #008080">strtolower</span>(<span style="color: #800080">$postObj</span>->Event) == 'subscribe'<span style="color: #000000">){
</span><span style="color: #008000">//</span><span style="color: #008000">回复用户消息</span>
<span style="color: #800080">$arr</span> = <span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'title' => '欢迎关注子钦的公众号开发版哟^^~~^^','description' => '张子钦很酷哦 or ziqin is very cool 哈哈!关注有惊喜哦','picUrl' => 'http://waphd.simufengyun.com/Public/Home/images/zmd123.png','url' => 'http://wap.simufengyun.com/',<span style="color: #000000">
)</span>,<span style="color: #000000">
);
</span><span style="color: #008000">//</span><span style="color: #008000">实例化模型</span>
<span style="color: #800080">$weChatModel</span> = <span style="color: #0000ff">new</span> \Home\Model\WeChatModel;<span style="color: #008000">//</span><span style="color: #008000">实例化时,一定要注意Model的路径问题 下面a()方法是测试路径问题</span>
<span style="color: #800080">$weChatModel</span>->responseSubscribe(<span style="color: #800080">$postObj</span>,<span style="color: #800080">$arr</span>);<span style="color: #008000">//</span><span style="color: #008000">调用模型中的方法</span>
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'click'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">如果是自定义菜单中的event->click
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->EventKey) == 'item1'<span style="color: #000000">){
<span style="color: #800080">$content = "这是菜单一事件的推送"<span style="color: #000000">;
}
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->EventKey) == 'songs'<span style="color: #000000">){
<span style="color: #800080">$content = "这是歌曲事件的推送"<span style="color: #000000">;
}
<span style="color: #008000">//<span style="color: #008000">实例化模型
//实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #008000">//<span style="color: #008000">跳转事件
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'view'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">如果是自定义菜单中的event->view
<span style="color: #800080">$content = "要跳转的链接是".<span style="color: #800080">$postObj-><span style="color: #000000">EventKey;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #008000">//
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'SCAN'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->scan
<span style="color: #800080">$content = "扫描场景".<span style="color: #800080">$postObj-><span style="color: #000000">EventKey;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'LOCATION'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->LOCATION
<span style="color: #800080">$content = "上传位置:纬度".<span style="color: #800080">$postObj->Latitude.";经度".<span style="color: #800080">$postObj-><span style="color: #000000">Longitude;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'scancode_waitmsg'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->scancode_waitmsg
<span style="color: #800080">$content = "扫描带提示:类型".<span style="color: #800080">$postObj->ScanCodeInfo->ScanType."结果:".<span style="color: #800080">$postObj->ScanCodeInfo-><span style="color: #000000">ScanResult;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'scancode_push'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->scancode_push
<span style="color: #800080">$content = "扫码推事件"<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'pic_sysphoto'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->pic_sysphoto
<span style="color: #800080">$content = "系统拍照"<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'pic_weixin'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->pic_weixin
<span style="color: #800080">$content = "相册发图:数量".<span style="color: #800080">$postObj->SendPicsInfo-><span style="color: #008080">Count<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'pic_photo_or_album'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->pic_photo_or_album
<span style="color: #800080">$content = "拍照或者相册:数量".<span style="color: #800080">$postObj->SendPicsInfo-><span style="color: #008080">Count<span style="color: #000000">;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
<span style="color: #0000ff">if(<span style="color: #008080">strtolower(<span style="color: #800080">$postObj->Event) == 'location_select'<span style="color: #000000">){
<span style="color: #008000">//<span style="color: #008000">自定义菜单中的event->location_select
<span style="color: #800080">$content = "发送位置:标签".<span style="color: #800080">$postObj->SendLocationInfo-><span style="color: #000000">Label;
<span style="color: #008000">//<span style="color: #008000">实例化模型
<span style="color: #800080">$weChatModel = <span style="color: #0000ff">new<span style="color: #000000"> \Home\Model\WeChatModel;
<span style="color: #800080">$weChatModel->responseText(<span style="color: #800080">$postObj,<span style="color: #800080">$content);<span style="color: #008000">//<span style="color: #008000">调用方法
<span style="color: #000000"> }
}
</span><span style="color: #008000">//</span><span style="color: #008000">判断是否是纯文本</span>
<span style="color: #008000">/*</span><span style="color: #008000">if(strtolower($postObj->MsgType) == 'text'){
switch(trim($postObj->Content)){
case 1:
$content = '您输入的数字是1';
break;
case 2:
$content = '您输入的数字是2';
break;
case 3:
$content = '您输入的数字是3';
break;
case '英文':
$content = 'ziqin is very good';
break;
case '电话':
$content = '15236272691';
break;
case '百度':
$content = '<a href="https://www.baidu.com">百度搜索</a>';
break;
}
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
echo sprintf($template,$toUser,$fromUser,$time,$msgType,$content);
}</span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>发送tuwen1关键字的时候,发送一个图文 或者发送纯文本 或者多图文</span>
<span style="color: #0000ff">if</span>(<span style="color: #008080">strtolower</span>(<span style="color: #800080">$postObj</span>->MsgType) == 'text' && <span style="color: #008080">trim</span>(<span style="color: #800080">$postObj</span>->Content)=='tuwen1'<span style="color: #000000">){
</span><span style="color: #800080">$arr</span> = <span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'title' => '私募风云网年度峰会报名开始啦','description' => 'ziqin is very cool','picUrl' => 'http://wap.simufengyun.com//Public/Home/images/ban1.jpg',<span style="color: #000000">
)</span>,<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'title' => '私募风云网官方网站','description' => 'simufengyun is very cool','picUrl' => 'http://wap.simufengyun.com//Public/Home/images/ban2.jpg',<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'title' => '我是子钦有话说','picUrl' => 'http://wap.simufengyun.com//Public/Home/images/ban3.jpg',<span style="color: #000000">
);
</span><span style="color: #008000">//</span><span style="color: #008000">实例化模型</span>
<span style="color: #800080">$weChatModel</span> = <span style="color: #0000ff">new</span> \Home\Model\WeChatModel;<span style="color: #008000">//</span><span style="color: #008000">实例化时,一定要注意Model的路径问题 下面a()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>是测试路径问题</span>
<span style="color: #800080">$weChatModel</span>->responseNews(<span style="color: #800080">$postObj</span>,<span style="color: #800080">$arr</span>);<span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>模型中的<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
}<span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #0000ff">switch</span>(<span style="color: #008080">trim</span>(<span style="color: #800080">$postObj</span>-><span style="color: #000000">Content)){
</span><span style="color: #0000ff">case</span> 1:
<span style="background-color: #ff0000; color: #ffffff; font-size: 18px"> $openId = $postObj->FromUserName;</span><span style="font-size: 18px">//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>openId</span></pre>
回复随机7位数验证码 $content .='您的openId是:'.$openId;数据库
2:
= '您输入的数字是2' 3:
= '您输入的数字是3' 4:
= '张' '英文':
= 'ziqin is very good' '电话':
= '15236272691' '百度':
= '百度搜索':
= "没有找到相关信息" </span><span style="color: #008000">//</span><span style="color: #008000">实例化模型</span>
<span style="color: #800080">$weChatModel</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> \Home\Model\WeChatModel;
</span><span style="color: #800080">$weChatModel</span>->responseText(<span style="color: #800080">$postObj</span>,<span style="color: #800080">$content</span>);<span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a><a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a></span>
<span style="color: #000000"> }
}
</span><span style="color: #008000">//</span><span style="color: #008000">接收事件消息</span>
<span style="color: #008000">/*<span style="color: #008000"> private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注方倍工作室 ";
$content .= (!empty($object->EventKey))?("\n来自二维码场景 ".strreplace("qrscene","",$object->EventKey)):"";
break;
case "unsubscribe":
$content = "取消关注";
break;
case "CLICK":
switch ($object->EventKey)
{
case "COMPANY":
$content = array();
$content[] = array("Title"=>"方倍工作室","Description"=>"","PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg","Url" =>"http://m.cnblogs.com/?u=txw1958");
break;
default:
$content = "点击菜单:".$object->EventKey;
break;
}
break;
case "VIEW":
$content = "跳转链接 ".$object->EventKey;
break;
case "SCAN":
$content = "扫描场景 ".$object->EventKey;
break;
case "LOCATION":
$content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
break;
case "scancode_waitmsg":
$content = "扫码带提示:类型 ".$object->ScanCodeInfo->ScanType." 结果:".$object->ScanCodeInfo->ScanResult;
break;
case "scancode_push":
$content = "扫码推事件";
break;
case "pic_sysphoto":
$content = "系统拍照";
break;
case "pic_weixin":
$content = "相册发图:数量 ".$object->SendPicsInfo->Count;
break;
case "pic_photo_or_album":
$content = "拍照或者相册:数量 ".$object->SendPicsInfo->Count;
break;
case "location_select":
$content = "发送位置:标签 ".$object->SendLocationInfo->Label;
break;
default:
$content = "receive a new event: ".$object->Event." \n技术支持 方倍工作室";
break;
}
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object,$content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object,$content);
}
}else{
$result = $this->transmitText($object,$content);
}
return $result;
}</span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000">采集远程数据</span>
<span style="color: #008000">/*</span><span style="color: #008000">function http_curl(){
//<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>远程数据
//1.初始化curl
$ch = curl_init();
$url = 'http://wap.simufengyun.com';
//2.设置curl参数
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//3.采集
$output = curl_exec($ch);
//4.<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>
curl_close($ch);
var_dump($output);
}</span><span style="color: #008000">*/</span>
<span style="color: #008000">/*</span><span style="color: #008000">*
*$url 接口url string
*$type 请求类型 string
*$res 返回数据类型 string
*$arr post请求参数 string
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span> http_curl(<span style="color: #800080">$url</span>,<span style="color: #800080">$type</span>='get',<span style="color: #800080">$res</span>='json',<span style="color: #800080">$arr</span>=''<span style="color: #000000">){
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>远程数据
//1.初始化curl</span>
<span style="color: #800080">$ch</span> =<span style="color: #000000"> curl_init();
</span><span style="color: #008000">//</span><span style="color: #008000">$url = 'http://wap.simufengyun.com';
//2.设置curl参数</span>
curl_setopt(<span style="color: #800080">$ch</span>,<span style="color: #800080">$url</span><span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,1<span style="color: #000000">);
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$type</span> == 'post'<span style="color: #000000">){
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_POST,1<span style="color: #000000">);
curl_setopt(</span><span style="color: #800080">$ch</span>,CURLOPT_POSTFIELDS,<span style="color: #800080">$arr</span><span style="color: #000000">);
}
</span><span style="color: #008000">//</span><span style="color: #008000">3.采集</span>
<span style="color: #800080">$output</span> = curl_exec(<span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">4.<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a></span>
curl_close(<span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span>(<span style="color: #800080">$res</span> == 'json'<span style="color: #000000">){
</span><span style="color: #0000ff">if</span>(curl_error(<span style="color: #800080">$ch</span><span style="color: #000000">)){
</span><span style="color: #008000">//</span><span style="color: #008000">请求失败,返回<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>信息</span>
<span style="color: #0000ff">return</span> curl_error(<span style="color: #800080">$ch</span><span style="color: #000000">);
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #0000ff">return</span> json_decode(<span style="color: #800080">$output</span>,<span style="color: #0000ff">true</span><span style="color: #000000">);
}
}
</span><span style="color: #008000">//</span><span style="color: #008000">var_dump($output);</span>
<span style="color: #000000"> }
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token和微信服务器ip地址</span>
<span style="color: #008000">/*</span><span style="color: #008000">function getWxAccessToken(){
//1.请求<a href="https://www.jb51.cc/tag/URLdizhi/" target="_blank" class="keywords">URL地址</a>
$appid = 'wxd3a57f24b4e021b2';
$appsecret = 'feffb8659a14792c9323fdf27b4b35f7';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
//2.初始化
$ch = curl_init();
//3.设置参数
curl_setopt($ch,1);
//4.<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>接口(采集)
$res = curl_exec($ch);
//5.<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>curl
curl_close($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$arr = json_decode($res,true);
var_dump($arr);
}</span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>微信服务器ip</span>
<span style="color: #0000ff">function</span><span style="color: #000000"> getWxServerIp(){
</span><span style="color: #800080">$accessToken</span> = "9E_hA1sd77bpRQ0GqhyU4G5zpLxKZM3Yrxa1za0yyYQpWJmXif5AI3nRNUBe6xESQtQR5SbHHTkvYx5XkcnXkcXn6gVpGVGGbCswbLMMtqEfD1mGXHobq3nKtUkwFXvVSLXgABAFYK"<span style="color: #000000">;
</span><span style="color: #800080">$url</span> = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".<span style="color: #800080">$accessToken</span><span style="color: #000000">;
</span><span style="color: #800080">$ch</span> =<span style="color: #000000"> curl_init();
curl_setopt(</span><span style="color: #800080">$ch</span>,1<span style="color: #000000">);
</span><span style="color: #800080">$res</span> = curl_exec(<span style="color: #800080">$ch</span><span style="color: #000000">);
curl_close(</span><span style="color: #800080">$ch</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span>(curl_errno(<span style="color: #800080">$ch</span><span style="color: #000000">)){
</span><span style="color: #008080">var_dump</span>(curl_error(<span style="color: #800080">$ch</span><span style="color: #000000">));
}
</span><span style="color: #800080">$arr</span> = json_decode(<span style="color: #800080">$res</span>,<span style="color: #0000ff">true</span><span style="color: #000000">);
</span><span style="color: #0000ff">echo</span> "<pre>"<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(<span style="color: #800080">$arr</span><span style="color: #000000">);
</span><span style="color: #0000ff">echo</span> "<pre>"<span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000">测试实例化Model模型路径问题 </span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> a(){
</span><span style="color: #800080">$weChatModel</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> \Home\Model\WeChatModel;
</span><span style="color: #008080">var_dump</span>(<span style="color: #800080">$weChatModel</span><span style="color: #000000">);
}
</span><span style="color: #008000">//</span><span style="color: #008000">返回access_token; access_token存入session避免重复<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a></span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> getWxAccessToken(){
</span><span style="color: #008000">//</span><span style="color: #008000">将access_token 存在session/cookie中</span>
<span style="color: #0000ff">if</span>(<span style="color: #800080">$_SESSION</span>['access_token'] && <span style="color: #800080">$_SESSION</span>['expires_time']><span style="color: #008080">time</span><span style="color: #000000">()){
</span><span style="color: #008000">//</span><span style="color: #008000">如果access_token在session并没有过期</span>
<span style="color: #0000ff">return</span> <span style="color: #800080">$_SESSION</span>['access_token'<span style="color: #000000">];
}</span><span style="color: #0000ff">else</span><span style="color: #000000">{
</span><span style="color: #008000">//</span><span style="color: #008000">如果access_token在session不存在或者已经过期,重新取access_token</span>
<span style="color: #800080">$appid</span> = 'wxd3a'<span style="color: #000000">;//此处用你自己的appid
</span><span style="color: #800080">$appsecret</span> = 'feffb8659a14792c9323fdf'<span style="color: #000000">;//用你自己的appsecret
</span><span style="color: #800080">$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".<span style="color: #800080">$appid</span>."&secret=".<span style="color: #800080">$appsecret</span><span style="color: #000000">;
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$this</span>->http_curl(<span style="color: #800080">$url</span>,'get','json'<span style="color: #000000">);
</span><span style="color: #800080">$access_token</span> = <span style="color: #800080">$res</span>['access_token'<span style="color: #000000">];
</span><span style="color: #008000">//</span><span style="color: #008000">将重新<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>到的access_token存入session</span>
<span style="color: #800080">$_SESSION</span>['access_token'] = <span style="color: #800080">$access_token</span><span style="color: #000000">;
</span><span style="color: #800080">$_SESSION</span>['expires_time'] = <span style="color: #008080">time</span>() + 7200<span style="color: #000000">;
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$access_token</span><span style="color: #000000">;
}
}
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/zidingyi/" target="_blank" class="keywords">自定义</a><a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span><span style="color: #000000"> definedItem(){
</span><span style="color: #008000">//</span><span style="color: #008000">创建微信<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a>
//目前微信接口的<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>方式都是通过curl post/get</span>
<span style="color: #008080">header</span>('content-type:text/html;charset=utf-8');<span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/jiejue/" target="_blank" class="keywords">解决</a><a href="https://www.jb51.cc/tag/zhongwen/" target="_blank" class="keywords">中文</a>乱码</span>
<span style="color: #0000ff">echo</span> <span style="color: #800080">$access_token</span> = <span style="color: #800080">$this</span>-><span style="color: #000000">getWxAccessToken();
</span><span style="color: #800080">$url</span> = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".<span style="color: #800080">$access_token</span><span style="color: #000000">;
</span><span style="color: #800080">$postArr</span> = <span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'button'=><span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>('子钦'),'type'=>'click','key'=>'item1',<span style="color: #008000">//</span><span style="color: #008000">第一个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>('子钦语录'),'sub_button'=><span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>('歌曲'),'key'=>'songs',<span style="color: #000000">
)</span>,<span style="color: #008000">//</span><span style="color: #008000">第一个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>('私募风云网'),'type'=>'view','url'=>'http://wap.simufengyun.com',<span style="color: #008000">//</span><span style="color: #008000">第二个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
),<span style="color: #000000">
)</span>,<span style="color: #008000">//</span><span style="color: #008000">第二个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name' =><span style="color: #008080">urlencode</span>('子钦心语'),'type' =>'view','url' => 'http://wap.simufengyun.com',<span style="color: #008000">//</span><span style="color: #008000">第三个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
),<span style="color: #000000">
);
</span><span style="color: #008000">//</span><span style="color: #008000">其他按钮类型</span>
<span style="color: #800080">$postArr2</span> = <span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'button'=><span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>('扫码'),'sub_button'=><span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'type'=>"scancode_waitmsg",'name'=><span style="color: #008080">urlencode</span>("扫码带<a href="https://www.jb51.cc/tag/tishi/" target="_blank" class="keywords">提示</a>"),'key'=>"rselfmenu_0_0",<span style="color: #000000">
)</span>,<span style="color: #008000">//</span><span style="color: #008000">第一个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'type'=>"scancode_push",'name'=><span style="color: #008080">urlencode</span>("扫码推事件"),'key'=>"rselfmenu_0_1",<span style="color: #008000">//</span><span style="color: #008000">第一个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>("发图"),'sub_button'=><span style="color: #0000ff">array</span><span style="color: #000000">(
</span><span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'type'=>'pic_sysphoto','name'=><span style="color: #008080">urlencode</span>("系统拍照发图"),'key'=>"rselfmenu_1_0",<span style="color: #008000">//</span><span style="color: #008000">第一个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'type'=>'pic_photo_or_album','name'=><span style="color: #008080">urlencode</span>("拍照或者相册发图"),'key'=>"rselfmenu_1_1",<span style="color: #008000">//</span><span style="color: #008000">第二个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'type'=>'pic_weixin','name'=><span style="color: #008080">urlencode</span>("微信相册发图"),'key'=>"rselfmenu_1_2",<span style="color: #008000">//</span><span style="color: #008000">第三个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
),<span style="color: #008000">//</span><span style="color: #008000">第二个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
<span style="color: #0000ff">array</span><span style="color: #000000">(
</span>'name'=><span style="color: #008080">urlencode</span>("发送位置"),'type'=>"location_select","key"=>"rselfmenu_2_0",<span style="color: #000000">
);
</span><span style="color: #008000">/*</span><span style="color: #008000">echo '<hr />';
var_dump($postArr);
echo '<hr />';
echo $postJson = urldecode(json_encode($postArr));
$res = $this->http_curl($url,'post','json',$postJson);
echo "<hr/>";
var_dump($res);</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">echo</span> '<hr />'<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(<span style="color: #800080">$postArr2</span><span style="color: #000000">);
</span><span style="color: #0000ff">echo</span> '<hr />'<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> <span style="color: #800080">$postJson</span> = <span style="color: #008080">urldecode</span>(json_encode(<span style="color: #800080">$postArr2</span><span style="color: #000000">));
</span><span style="color: #800080">$res</span> = <span style="color: #800080">$this</span>->http_curl(<span style="color: #800080">$url</span>,<span style="color: #800080">$postJson</span><span style="color: #000000">);
</span><span style="color: #0000ff">echo</span> "<hr/>"<span style="color: #000000">;
</span><span style="color: #008080">var_dump</span>(<span style="color: #800080">$res</span><span style="color: #000000">);
}
}</span></pre>
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huifu/" target="_blank" class="keywords">回复</a>单文本</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> responseText(<span style="color: #800080">$postObj</span>,<span style="color: #800080">$content</span><span style="color: #000000">){
</span><span style="color: #800080">$template</span> = "<span style="color: #000000"><xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml></span>"<span style="color: #000000">;
</span><span style="color: #800080">$toUser</span> = <span style="color: #800080">$postObj</span>-><span style="color: #000000">FromUserName;
</span><span style="color: #800080">$fromUser</span> = <span style="color: #800080">$postObj</span>-><span style="color: #000000">ToUserName;
</span><span style="color: #800080">$time</span> = <span style="color: #008080">time</span><span style="color: #000000">();
</span><span style="color: #800080">$msgType</span> = 'text'<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> <span style="color: #008080">sprintf</span>(<span style="color: #800080">$template</span>,<span style="color: #800080">$msgType</span>,<span style="color: #800080">$content</span><span style="color: #000000">);
}
</span><span style="color: #008000">//</span><span style="color: #008000"><a href="https://www.jb51.cc/tag/huifu/" target="_blank" class="keywords">回复</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>关注消息</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> responseSubscribe(<span style="color: #800080">$postObj</span>,<span style="color: #800080">$arr</span><span style="color: #000000">){
</span><span style="color: #800080">$this</span>->responseNews(<span style="color: #800080">$postObj</span>,<span style="color: #800080">$arr</span><span style="color: #000000">);
}
}
函数方法
有不明白的加微信咨询zmd1047773569
原文链接:https://www.f2er.com/wxmp/69498.html