微信公众号图文回复验证码并写入数据库的基本思路

前端之家收集整理的这篇文章主要介绍了微信公众号图文回复验证码并写入数据库的基本思路前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

接了个小活:要求公众号中用户回复1,公众号被动回复7位数验证码,同时把用户openId和验证码写入数据库

简单写了各测试demo:工具 thinkPHP   订阅号子钦有话说内申请的测试账号 

接口配置信息修改

WeChatController = ['nonce' = 'zhangmangde' = ['timestamp' = ['echostr' = ['signature' = = (,,( = ((( == && ->} </span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;public function show(){ echo 'zhangmangde'; }</span><span style="color: #008000"&gt;*/</span> <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;接受事件,推送并回复</span> <span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; responseMsg(){ </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;1.获取到微信推送过来的post数据(xml格式)</span> <span style="color: #800080"&gt;$postArr</span> = <span style="color: #800080"&gt;$GLOBALS</span>['HTTP_RAW_POST_DATA'<span style="color: #000000"&gt;]; </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;2.处理消息类型,并设置回复内容和类型</span> <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;<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"&gt;*/</span> <span style="color: #800080"&gt;$postObj</span> = <span style="color: #008080"&gt;simplexml_load_string</span>(<span style="color: #800080"&gt;$postArr</span>);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;将xml字符串载入对象中 //$postObj->ToUserName = ''; //$postObj->FromUserName = ''; //$postObj->CreateTime = ''; //$postObj->MsgType = ''; //$postObj->Event = ''; //判断该数据包是否是订阅的事件推送</span> <span style="color: #0000ff"&gt;if</span>(<span style="color: #008080"&gt;strtolower</span>(<span style="color: #800080"&gt;$postObj</span>->MsgType) == 'event'<span style="color: #000000"&gt;){ </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;如果是关注subcribe事件</span> <span style="color: #0000ff"&gt;if</span>(<span style="color: #008080"&gt;strtolower</span>(<span style="color: #800080"&gt;$postObj</span>->Event) == 'subscribe'<span style="color: #000000"&gt;){ </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;回复用户消息</span> <span style="color: #800080"&gt;$arr</span> = <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;( </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;( </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"&gt; )</span>,<span style="color: #000000"&gt; ); </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;实例化模型</span> <span style="color: #800080"&gt;$weChatModel</span> = <span style="color: #0000ff"&gt;new</span> \Home\Model\WeChatModel;<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;实例化时,一定要注意Model的路径问题 下面a()方法是测试路径问题</span> <span style="color: #800080"&gt;$weChatModel</span>->responseSubscribe(<span style="color: #800080"&gt;$postObj</span>,<span style="color: #800080"&gt;$arr</span>);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;调用模型中的方法</span>

<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) == '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"&gt;//</span><span style="color: #008000"&gt;判断是否是纯文本</span>
    <span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;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"&gt;百度搜索</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"&gt;*/</span>

    <span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>发送tuwen1关键字的时候,发送一个图文 或者发送纯文本  或者多图文</span>
    <span style="color: #0000ff"&gt;if</span>(<span style="color: #008080"&gt;strtolower</span>(<span style="color: #800080"&gt;$postObj</span>->MsgType) == 'text' &amp;&amp; <span style="color: #008080"&gt;trim</span>(<span style="color: #800080"&gt;$postObj</span>->Content)=='tuwen1'<span style="color: #000000"&gt;){
        </span><span style="color: #800080"&gt;$arr</span>  = <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
            </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'title'       => '私募风云网年度峰会报名开始啦','description' => 'ziqin is very cool','picUrl'      => 'http://wap.simufengyun.com//Public/Home/images/ban1.jpg',<span style="color: #000000"&gt;
            )</span>,<span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'title'       => '私募风云网官方网站','description' => 'simufengyun is very cool','picUrl'      => 'http://wap.simufengyun.com//Public/Home/images/ban2.jpg',<span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'title'       => '我是子钦有话说','picUrl'      => 'http://wap.simufengyun.com//Public/Home/images/ban3.jpg',<span style="color: #000000"&gt;
        );
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;实例化模型</span>
        <span style="color: #800080"&gt;$weChatModel</span> = <span style="color: #0000ff"&gt;new</span> \Home\Model\WeChatModel;<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;实例化时,一定要注意Model的路径问题 下面a()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>是测试路径问题</span>
        <span style="color: #800080"&gt;$weChatModel</span>->responseNews(<span style="color: #800080"&gt;$postObj</span>,<span style="color: #800080"&gt;$arr</span>);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<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"&gt;else</span><span style="color: #000000"&gt;{
        </span><span style="color: #0000ff"&gt;switch</span>(<span style="color: #008080"&gt;trim</span>(<span style="color: #800080"&gt;$postObj</span>-><span style="color: #000000"&gt;Content)){
            </span><span style="color: #0000ff"&gt;case</span> 1:
               <span style="background-color: #ff0000; color: #ffffff; font-size: 18px"&gt;   $openId = $postObj->FromUserName;</span><span style="font-size: 18px"&gt;//<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"&gt;//</span><span style="color: #008000"&gt;实例化模型</span>
        <span style="color: #800080"&gt;$weChatModel</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; \Home\Model\WeChatModel;
        </span><span style="color: #800080"&gt;$weChatModel</span>->responseText(<span style="color: #800080"&gt;$postObj</span>,<span style="color: #800080"&gt;$content</span>);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<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"&gt;//</span><span style="color: #008000"&gt;接收事件消息</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"&gt;*/</span>

<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;采集远程数据</span>
<span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;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"&gt;*/</span>

<span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;*
*$url 接口url string
*$type 请求类型 string
*$res 返回数据类型 string
*$arr post请求参数 string
</span><span style="color: #008000"&gt;*/</span>
<span style="color: #0000ff"&gt;function</span> http_curl(<span style="color: #800080"&gt;$url</span>,<span style="color: #800080"&gt;$type</span>='get',<span style="color: #800080"&gt;$res</span>='json',<span style="color: #800080"&gt;$arr</span>=''<span style="color: #000000"&gt;){
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>远程数据
    //1.初始化curl</span>
    <span style="color: #800080"&gt;$ch</span>  =<span style="color: #000000"&gt; curl_init();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;$url = 'http://wap.simufengyun.com';
    //2.设置curl参数</span>
    curl_setopt(<span style="color: #800080"&gt;$ch</span>,<span style="color: #800080"&gt;$url</span><span style="color: #000000"&gt;);
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,1<span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$type</span> == 'post'<span style="color: #000000"&gt;){
        curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_POST,1<span style="color: #000000"&gt;);
        curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,CURLOPT_POSTFIELDS,<span style="color: #800080"&gt;$arr</span><span style="color: #000000"&gt;);
    }
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;3.采集</span>
    <span style="color: #800080"&gt;$output</span> = curl_exec(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;4.<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a></span>
    curl_close(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$res</span> == 'json'<span style="color: #000000"&gt;){
        </span><span style="color: #0000ff"&gt;if</span>(curl_error(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;)){
            </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;请求失败,返回<a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>信息</span>
            <span style="color: #0000ff"&gt;return</span> curl_error(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
        }</span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;{
            </span><span style="color: #0000ff"&gt;return</span> json_decode(<span style="color: #800080"&gt;$output</span>,<span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;);
        }

    }
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;var_dump($output);</span>

<span style="color: #000000"> }

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token和微信服务器ip地址</span>
<span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;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&amp;appid=".$appid."&amp;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"&gt;*/</span>
<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>微信服务器ip</span>
<span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; getWxServerIp(){
    </span><span style="color: #800080"&gt;$accessToken</span> = "9E_hA1sd77bpRQ0GqhyU4G5zpLxKZM3Yrxa1za0yyYQpWJmXif5AI3nRNUBe6xESQtQR5SbHHTkvYx5XkcnXkcXn6gVpGVGGbCswbLMMtqEfD1mGXHobq3nKtUkwFXvVSLXgABAFYK"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$url</span> = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".<span style="color: #800080"&gt;$accessToken</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$ch</span> =<span style="color: #000000"&gt; curl_init();
    curl_setopt(</span><span style="color: #800080"&gt;$ch</span>,1<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$res</span> = curl_exec(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
    curl_close(</span><span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;if</span>(curl_errno(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;)){
        </span><span style="color: #008080"&gt;var_dump</span>(curl_error(<span style="color: #800080"&gt;$ch</span><span style="color: #000000"&gt;));
    }
    </span><span style="color: #800080"&gt;$arr</span> = json_decode(<span style="color: #800080"&gt;$res</span>,<span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;echo</span> "<pre>"<span style="color: #000000"&gt;;
    </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$arr</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;echo</span> "<pre>"<span style="color: #000000"&gt;;

}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;测试实例化Model模型路径问题 </span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; a(){
    </span><span style="color: #800080"&gt;$weChatModel</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; \Home\Model\WeChatModel;
    </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$weChatModel</span><span style="color: #000000"&gt;);
}

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;返回access_token;  access_token存入session避免重复<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a></span>
<span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; getWxAccessToken(){
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;将access_token 存在session/cookie中</span>
    <span style="color: #0000ff"&gt;if</span>(<span style="color: #800080"&gt;$_SESSION</span>['access_token'] &amp;&amp; <span style="color: #800080"&gt;$_SESSION</span>['expires_time']><span style="color: #008080"&gt;time</span><span style="color: #000000"&gt;()){
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;如果access_token在session并没有过期</span>
        <span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$_SESSION</span>['access_token'<span style="color: #000000"&gt;];
    }</span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;{
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;如果access_token在session不存在或者已经过期,重新取access_token</span>
        <span style="color: #800080"&gt;$appid</span>   = 'wxd3a'<span style="color: #000000"&gt;;//此处用你自己的appid
        </span><span style="color: #800080"&gt;$appsecret</span> = 'feffb8659a14792c9323fdf'<span style="color: #000000"&gt;;//用你自己的appsecret
        </span><span style="color: #800080"&gt;$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=".<span style="color: #800080"&gt;$appid</span>."&amp;secret=".<span style="color: #800080"&gt;$appsecret</span><span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$this</span>->http_curl(<span style="color: #800080"&gt;$url</span>,'get','json'<span style="color: #000000"&gt;);
        </span><span style="color: #800080"&gt;$access_token</span> = <span style="color: #800080"&gt;$res</span>['access_token'<span style="color: #000000"&gt;];
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;将重新<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>到的access_token存入session</span>
        <span style="color: #800080"&gt;$_SESSION</span>['access_token'] = <span style="color: #800080"&gt;$access_token</span><span style="color: #000000"&gt;;
        </span><span style="color: #800080"&gt;$_SESSION</span>['expires_time']  = <span style="color: #008080"&gt;time</span>() + 7200<span style="color: #000000"&gt;;
        </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$access_token</span><span style="color: #000000"&gt;;
    }
}
</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<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"&gt;public</span> <span style="color: #0000ff"&gt;function</span><span style="color: #000000"&gt; definedItem(){
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;创建微信<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"&gt;header</span>('content-type:text/html;charset=utf-8');<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<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"&gt;echo</span> <span style="color: #800080"&gt;$access_token</span> = <span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;getWxAccessToken();
    </span><span style="color: #800080"&gt;$url</span> = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".<span style="color: #800080"&gt;$access_token</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$postArr</span> = <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
        </span>'button'=><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
            </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'name'=><span style="color: #008080"&gt;urlencode</span>('子钦'),'type'=>'click','key'=>'item1',<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第一个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
            <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'name'=><span style="color: #008080"&gt;urlencode</span>('子钦语录'),'sub_button'=><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                    </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'name'=><span style="color: #008080"&gt;urlencode</span>('歌曲'),'key'=>'songs',<span style="color: #000000"&gt;
                    )</span>,<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第一个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
                    <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'name'=><span style="color: #008080"&gt;urlencode</span>('私募风云网'),'type'=>'view','url'=>'http://wap.simufengyun.com',<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第二个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
                ),<span style="color: #000000"&gt;

            )</span>,<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第二个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
            <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'name' =><span style="color: #008080"&gt;urlencode</span>('子钦心语'),'type' =>'view','url' => 'http://wap.simufengyun.com',<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第三个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
        ),<span style="color: #000000"&gt;

    );
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;其他按钮类型</span>
    <span style="color: #800080"&gt;$postArr2</span> = <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
        </span>'button'=><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
            </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'name'=><span style="color: #008080"&gt;urlencode</span>('扫码'),'sub_button'=><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                    </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'type'=>"scancode_waitmsg",'name'=><span style="color: #008080"&gt;urlencode</span>("扫码带<a href="https://www.jb51.cc/tag/tishi/" target="_blank" class="keywords">提示</a>"),'key'=>"rselfmenu_0_0",<span style="color: #000000"&gt;

                    )</span>,<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第一个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
                    <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'type'=>"scancode_push",'name'=><span style="color: #008080"&gt;urlencode</span>("扫码推事件"),'key'=>"rselfmenu_0_1",<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第一个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
            <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'name'=><span style="color: #008080"&gt;urlencode</span>("发图"),'sub_button'=><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                    </span><span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'type'=>'pic_sysphoto','name'=><span style="color: #008080"&gt;urlencode</span>("系统拍照发图"),'key'=>"rselfmenu_1_0",<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第一个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
                    <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'type'=>'pic_photo_or_album','name'=><span style="color: #008080"&gt;urlencode</span>("拍照或者相册发图"),'key'=>"rselfmenu_1_1",<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第二个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
                    <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                        </span>'type'=>'pic_weixin','name'=><span style="color: #008080"&gt;urlencode</span>("微信相册发图"),'key'=>"rselfmenu_1_2",<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第三个二级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
                ),<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;第二个一级<a href="https://www.jb51.cc/tag/caidan/" target="_blank" class="keywords">菜单</a></span>
            <span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(
                </span>'name'=><span style="color: #008080"&gt;urlencode</span>("发送位置"),'type'=>"location_select","key"=>"rselfmenu_2_0",<span style="color: #000000"&gt;
    );
    </span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;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"&gt;*/</span>
    <span style="color: #0000ff"&gt;echo</span> '<hr />'<span style="color: #000000"&gt;;
    </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$postArr2</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;echo</span> '<hr />'<span style="color: #000000"&gt;;
    </span><span style="color: #0000ff"&gt;echo</span> <span style="color: #800080"&gt;$postJson</span> = <span style="color: #008080"&gt;urldecode</span>(json_encode(<span style="color: #800080"&gt;$postArr2</span><span style="color: #000000"&gt;));
    </span><span style="color: #800080"&gt;$res</span> = <span style="color: #800080"&gt;$this</span>->http_curl(<span style="color: #800080"&gt;$url</span>,<span style="color: #800080"&gt;$postJson</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;echo</span> "<hr/>"<span style="color: #000000"&gt;;
    </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$res</span><span style="color: #000000"&gt;);
}
}</span></pre>

PHP namespace Home\Model; WeChatModel 回复微信方法 responseNews(, = -> = -> = = " %s ".()." "( => .=" <![CDATA[</span>".<span style="color: #800080">$v</span>['title']."<span style="color: #000000">]]> ".['description']." ".['picUrl']." ".['url']." " .= " " (,,,,'news' </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/huifu/" target="_blank" class="keywords">回复</a>单文本</span> <span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;function</span> responseText(<span style="color: #800080"&gt;$postObj</span>,<span style="color: #800080"&gt;$content</span><span style="color: #000000"&gt;){ </span><span style="color: #800080"&gt;$template</span> = "<span style="color: #000000"&gt;<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"&gt;; </span><span style="color: #800080"&gt;$toUser</span> = <span style="color: #800080"&gt;$postObj</span>-><span style="color: #000000"&gt;FromUserName; </span><span style="color: #800080"&gt;$fromUser</span> = <span style="color: #800080"&gt;$postObj</span>-><span style="color: #000000"&gt;ToUserName; </span><span style="color: #800080"&gt;$time</span> = <span style="color: #008080"&gt;time</span><span style="color: #000000"&gt;(); </span><span style="color: #800080"&gt;$msgType</span> = 'text'<span style="color: #000000"&gt;; </span><span style="color: #0000ff"&gt;echo</span> <span style="color: #008080"&gt;sprintf</span>(<span style="color: #800080"&gt;$template</span>,<span style="color: #800080"&gt;$msgType</span>,<span style="color: #800080"&gt;$content</span><span style="color: #000000"&gt;); } </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<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"&gt;public</span> <span style="color: #0000ff"&gt;function</span> responseSubscribe(<span style="color: #800080"&gt;$postObj</span>,<span style="color: #800080"&gt;$arr</span><span style="color: #000000"&gt;){ </span><span style="color: #800080"&gt;$this</span>->responseNews(<span style="color: #800080"&gt;$postObj</span>,<span style="color: #800080"&gt;$arr</span><span style="color: #000000"&gt;); }

}

函数方法

生成随机数,length长度 getsRandChar( = = "0123456789abcdefghijklmnopqrstuvwxyz" = ()-1(=0;<;++.=[(0,)];生成介于min和max两个数之间的一个随机整数

有不明白的加微信咨询zmd1047773569

原文链接:https://www.f2er.com/wxmp/69498.html

猜你在找的微信公众号相关文章