Heim >php教程 >php手册 >我也写了个公众微信的

我也写了个公众微信的

WBOY
WBOYOriginal
2016-06-07 11:43:13996Durchsuche

基于THINKPHP的公众微信开发,自己写的代码的,写的不好,轻喷
<?php <br /> <br> /**<br>  * @author zhiwupei<br>  * <br>  *<br>  */<br> class WupeiAction extends Action<br> {<br>     //微信、易信验证token<br>     public $token='wupei';<br>     //微信、易信提交过来的内容<br>     public $postObj;<br>     //用户回复的内容<br>     public $keyword;<br>     //用户关注事件<br>     public $Event;<br>     //点击菜单事件<br>     public $evenkey;<br>     //公众微信ID号<br>     public $myid;<br>     //用户ID号<br>     public $userid;<br>     //回复XML模板<br>     public $xml;<br>     //回复消息类型<br>     public $msgType;<br>     //微信提供的APPID<br>     public $APPID;<br>     //微信提供的APPSECRET<br>     public $APPSECRET;<br>     public $PicUrl;<br>     public $MsgType;<br>     public $Location_X;<br>     public $Location_Y;<br>     public $textTpl;<br>     function _initialize(){<br>         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];<br>         if (!empty($postStr)){<br>             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br>             $this->Event = $postObj->Event;   //关注事件<br>             $this->evenkey = $postObj->EventKey;//菜单事件<br>             $this->myid = $postObj->ToUserName;<br>             $this->userid = $postObj->FromUserName;<br>             $this->PicUrl=$postObj->PicUrl;<br>             $this->MsgType=$postObj->MsgType;<br>             if($this->MsgType=="text"){<br>                 $this->keyword = trim($postObj->Content);<br>             }else if($this->MsgType=="location"){<br>                 $this->Location_X=$postObj->Location_X;<br>                 $this->Location_Y=$postObj->Location_Y;<br>             }<br>         }<br>     }<br>     function index(){<br>         //如果 是验证的时候,请把下面一行的注释去了<br>        // $this->valid();<br>         if($this->MsgType=="text"){<br>             $this->sendMessage($this->keyword);<br>         }else if($this->MsgType=="location"){<br>             <br>         }<br>     }<br>     /**<br>      * @param 回复消息的内容 $content<br>      *<br>      */<br>     function sendMessage($content){<br> <br>         $this->textTpl = "<xml><br>                 <tousername></tousername><br>                 <fromusername></fromusername><br>                 <createtime>%s</createtime><br>                 <msgtype></msgtype><br>                 <content></content><br>                 <funcflag>0</funcflag><br>                 </xml>";<br>         $this->msgType = "text";<br>         $resultStr = sprintf($this->textTpl, $this->userid, $this->myid, time(), $this->msgType, $content);<br>         echo $resultStr;<br>         exit();<br> <br>     }<br>     /**<br>      * @param unknown $arr<br>      * $arr包括title标题,intro简介,imagepath图片路径,url访问路径,四个属性<br>      * 图片路径用绝对路径,即http://开头<br>      */<br>     function sendNews($arr){<br>         $this->msgType = "news";<br>         $this->textTpl="<xml><br>                 <tousername>userid."]]></tousername><br>                         <fromusername>myid."]]></fromusername><br>                                 <createtime>".time()."</createtime><br>                                         <msgtype>msgType."]]></msgtype><br>                                                 <articlecount>".count($arr)."</articlecount><br>                                                         <articles>";<br> <br>         foreach ($arr as $key =>$list){<br>             $this->textTpl.="<item><br>                     <title></title> <br>                             <description></description><br>                                     <picurl></picurl><br>                                     <url></url><br>                                     </item><br>                                     ";<br>         }<br>         $this->textTpl.="</articles></xml>";<br>         echo $this->textTpl;<br>         exit();<br>     }<br> <br>     /**<br>      *验证token方法<br>      */<br>     public function valid(){<br>         $echoStr = $_GET["echostr"];<br>         if($this->checkSignature()){<br>             echo $echoStr;<br>             exit;<br>         }<br>     }<br> <br>     function checkSignature(){<br>         $signature = $_GET["signature"];<br>         $timestamp = $_GET["timestamp"];<br>         $nonce = $_GET["nonce"];<br>         $tmpArr = array($this->token, $timestamp, $nonce);<br>         sort($tmpArr);<br>         $tmpStr = implode( $tmpArr );<br>         $tmpStr = sha1( $tmpStr );<br> <br>         if( $tmpStr == $signature ){<br>             return true;<br>         }else{<br>             return false;<br>         }<br>     }<br> <br>     function startmemu(){<br>         header("Content-type: text/html; charset=utf-8");<br>         $APPID = "wx01bee286793e0383";<br>         $APPSECRET = "84e324cb2daec6a8a1aea8b19cce465d";<br>         $ACCESS_TOKEN = "kgfind";<br>         $data = ' {<br>                 "button":[<br>                 {<br>                 "type":"click",<br>                 "name":"今日歌曲",<br>                 "key":"V1001_TODAY_MUSIC"<br>     },<br>     {<br>                 "type":"click",<br>                 "name":"歌手简介",<br>                 "key":"V1001_TODAY_SINGER"<br>     },<br>     {<br>                 "name":"菜单",<br>                 "sub_button":[<br>                 {<br>                 "type":"view",<br>                 "name":"搜索",<br>                 "url":"http://www.soso.com/"<br>     },<br>     {<br>                 "type":"view",<br>                 "name":"视频",<br>                 "url":"http://v.qq.com/"<br>     },<br>     {<br>                 "type":"click",<br>                 "name":"赞一下我们",<br>                 "key":"V1001_GOOD"<br>     }]<br>     }]<br>     }';<br> <br>         $ch = curl_init();<br>         curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET."");<br>         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");<br>         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br>         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);<br>         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');<br>         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);<br>         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);<br>         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<br>         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);<br>         $token = curl_exec($ch);<br>         if (curl_errno($ch)) {<br>             echo 'CURL error on:'.curl_error($ch);<br>         } else {<br>             $arr = json_decode($token, true);<br>             $ACCESS_TOKEN = $arr['access_token'];<br>             curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");<br>             $rs = curl_exec($ch);<br>             if (curl_errno($ch)) {<br>                 echo 'CURL error on:'.curl_error($ch);<br>             } else {<br>                 $arr = json_decode($rs, true);<br>                 echo "Code:" . $arr['errcode'];<br>                 echo "<br>Message:" . $arr['errmsg'];<br>             }<br>         }<br> <br>         curl_close($ch);<br>     }<br> }<br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Httpd.ini规则转.Htaccess 谢谢Nächster Artikel:基于CSS3的漂亮按钮