我也写了个公众微信的

jerry PHP 2015年11月18日 收藏
基于THINKPHP的公众微信开发,自己写的代码的,写的不好,轻喷
  1. <?php

  2. /**
  3.  * @author zhiwupei
  4.  * 
  5.  *
  6.  */
  7. class WupeiAction extends Action
  8. {
  9.     //微信、易信验证token
  10.     public $token='wupei';
  11.     //微信、易信提交过来的内容
  12.     public $postObj;
  13.     //用户回复的内容
  14.     public $keyword;
  15.     //用户关注事件
  16.     public $Event;
  17.     //点击菜单事件
  18.     public $evenkey;
  19.     //公众微信ID号
  20.     public $myid;
  21.     //用户ID号
  22.     public $userid;
  23.     //回复XML模板
  24.     public $xml;
  25.     //回复消息类型
  26.     public $msgType;
  27.     //微信提供的APPID
  28.     public $APPID;
  29.     //微信提供的APPSECRET
  30.     public $APPSECRET;
  31.     public $PicUrl;
  32.     public $MsgType;
  33.     public $Location_X;
  34.     public $Location_Y;
  35.     public $textTpl;
  36.     function _initialize(){
  37.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  38.         if (!empty($postStr)){
  39.             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  40.             $this->Event = $postObj->Event;   //关注事件
  41.             $this->evenkey = $postObj->EventKey;//菜单事件
  42.             $this->myid = $postObj->ToUserName;
  43.             $this->userid = $postObj->FromUserName;
  44.             $this->PicUrl=$postObj->PicUrl;
  45.             $this->MsgType=$postObj->MsgType;
  46.             if($this->MsgType=="text"){
  47.                 $this->keyword = trim($postObj->Content);
  48.             }else if($this->MsgType=="location"){
  49.                 $this->Location_X=$postObj->Location_X;
  50.                 $this->Location_Y=$postObj->Location_Y;
  51.             }
  52.         }
  53.     }
  54.     function index(){
  55.         //如果 是验证的时候,请把下面一行的注释去了
  56.        // $this->valid();
  57.         if($this->MsgType=="text"){
  58.             $this->sendMessage($this->keyword);
  59.         }else if($this->MsgType=="location"){
  60.             
  61.         }
  62.     }
  63.     /**
  64.      * @param 回复消息的内容 $content
  65.      *
  66.      */
  67.     function sendMessage($content){

  68.         $this->textTpl = "<xml>
  69.                 <ToUserName><![CDATA[%s]]></ToUserName>
  70.                 <FromUserName><![CDATA[%s]]></FromUserName>
  71.                 <CreateTime>%s</CreateTime>
  72.                 <MsgType><![CDATA[%s]]></MsgType>
  73.                 <Content><![CDATA[%s]]></Content>
  74.                 <FuncFlag>0</FuncFlag>
  75.                 </xml>";
  76.         $this->msgType = "text";
  77.         $resultStr = sprintf($this->textTpl, $this->userid, $this->myid, time(), $this->msgType, $content);
  78.         echo $resultStr;
  79.         exit();

  80.     }
  81.     /**
  82.      * @param unknown $arr
  83.      * $arr包括title标题,intro简介,imagepath图片路径,url访问路径,四个属性
  84.      * 图片路径用绝对路径,即http://开头
  85.      */
  86.     function sendNews($arr){
  87.         $this->msgType = "news";
  88.         $this->textTpl="<xml>
  89.                 <ToUserName><![CDATA[".$this->userid."]]></ToUserName>
  90.                         <FromUserName><![CDATA[".$this->myid."]]></FromUserName>
  91.                                 <CreateTime>".time()."</CreateTime>
  92.                                         <MsgType><![CDATA[".$this->msgType."]]></MsgType>
  93.                                                 <ArticleCount>".count($arr)."</ArticleCount>
  94.                                                         <Articles>";

  95.         foreach ($arr as $key =>$list){
  96.             $this->textTpl.="<item>
  97.                     <Title><![CDATA[".$list['title']."]]></Title>
  98.                             <Description><![CDATA[".$list['name']."]]></Description>
  99.                                     <PicUrl><![CDATA[".$list['imagapath']."]]></PicUrl>
  100.                                     <Url><![CDATA[".$list['url']."]]></Url>
  101.                                     </item>
  102.                                     ";
  103.         }
  104.         $this->textTpl.="</Articles></xml>";
  105.         echo $this->textTpl;
  106.         exit();
  107.     }

  108.     /**
  109.      *验证token方法
  110.      */
  111.     public function valid(){
  112.         $echoStr = $_GET["echostr"];
  113.         if($this->checkSignature()){
  114.             echo $echoStr;
  115.             exit;
  116.         }
  117.     }

  118.     function checkSignature(){
  119.         $signature = $_GET["signature"];
  120.         $timestamp = $_GET["timestamp"];
  121.         $nonce = $_GET["nonce"];
  122.         $tmpArr = array($this->token, $timestamp, $nonce);
  123.         sort($tmpArr);
  124.         $tmpStr = implode( $tmpArr );
  125.         $tmpStr = sha1( $tmpStr );

  126.         if( $tmpStr == $signature ){
  127.             return true;
  128.         }else{
  129.             return false;
  130.         }
  131.     }

  132.     function startmemu(){
  133.         header("Content-type: text/html; charset=utf-8");
  134.         $APPID = "wx01bee286793e0383";
  135.         $APPSECRET = "84e324cb2daec6a8a1aea8b19cce465d";
  136.         $ACCESS_TOKEN = "kgfind";
  137.         $data = ' {
  138.                 "button":[
  139.                 {
  140.                 "type":"click",
  141.                 "name":"今日歌曲",
  142.                 "key":"V1001_TODAY_MUSIC"
  143.     },
  144.     {
  145.                 "type":"click",
  146.                 "name":"歌手简介",
  147.                 "key":"V1001_TODAY_SINGER"
  148.     },
  149.     {
  150.                 "name":"菜单",
  151.                 "sub_button":[
  152.                 {
  153.                 "type":"view",
  154.                 "name":"搜索",
  155.                 "url":"http://www.soso.com/"
  156.     },
  157.     {
  158.                 "type":"view",
  159.                 "name":"视频",
  160.                 "url":"http://v.qq.com/"
  161.     },
  162.     {
  163.                 "type":"click",
  164.                 "name":"赞一下我们",
  165.                 "key":"V1001_GOOD"
  166.     }]
  167.     }]
  168.     }';

  169.         $ch = curl_init();
  170.         curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->APPID."&secret=".$this->APPSECRET."");
  171.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  172.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  173.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  174.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  175.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  176.         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  177.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  178.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  179.         $token = curl_exec($ch);
  180.         if (curl_errno($ch)) {
  181.             echo 'CURL error on:'.curl_error($ch);
  182.         } else {
  183.             $arr = json_decode($token, true);
  184.             $ACCESS_TOKEN = $arr['access_token'];
  185.             curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$ACCESS_TOKEN}");
  186.             $rs = curl_exec($ch);
  187.             if (curl_errno($ch)) {
  188.                 echo 'CURL error on:'.curl_error($ch);
  189.             } else {
  190.                 $arr = json_decode($rs, true);
  191.                 echo "Code:" . $arr['errcode'];
  192.                 echo "<br>Message:" . $arr['errmsg'];
  193.             }
  194.         }

  195.         curl_close($ch);
  196.     }
  197. }
  198. ?>