微信公众号入口文件示例

  1 <?php
  2 define("TOKEN", "weixin");
  3 $wechatObj = new wechatCallbackapiTest();
  4 //$wechatObj->valid();
  5 //$wechatObj->menu();
  6 //$wechatObj->delMenu();
  7 //$wechatObj->qrCode();
  8 $wechatObj->responseMsg();
  9 class wechatCallbackapiTest
 10 {
 11     protected     $appid="wx648efe06b5d56db6";
 12     protected    $secret="4a231637756bd650ed133710fa2eb746";
 13     public function valid()
 14     {
 15         $echoStr = $_GET["echostr"];
 16         if($this->checkSignature()){
 17             echo $echoStr;
 18             exit;
 19         }
 20     }    
 21     private function checkSignature()
 22     {
 23         if (!defined("TOKEN")) {
 24             throw new Exception('TOKEN is not defined!');
 25         }
 26         $signature = $_GET["signature"];
 27         $timestamp = $_GET["timestamp"];
 28         $nonce = $_GET["nonce"];
 29                 
 30         $token = TOKEN;
 31         $tmpArr = array($token, $timestamp, $nonce);
 32         sort($tmpArr, SORT_STRING);
 33         $tmpStr = implode( $tmpArr );
 34         $tmpStr = sha1( $tmpStr );
 35         
 36         if( $tmpStr == $signature ){
 37             return true;
 38         }else{
 39             return false;
 40         }
 41     }
 42     public function token(){
 43         $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
 44         $ch=curl_init();
 45         curl_setopt($ch,CURLOPT_URL,$url);
 46         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 47         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 48         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 49         $str=curl_exec($ch);
 50         if($str!= false)
 51         {
 52             $jsonstr=json_decode($str);
 53             return $jsonstr->access_token;
 54         }else{
 55             echo curl_error($ch);
 56         }
 57         curl_close($ch);
 58     }
 59     public function delMenu(){
 60         $url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->token();
 61         $ch=curl_init();
 62         curl_setopt($ch,CURLOPT_URL,$url);
 63         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 64         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 65         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 66         curl_exec($ch);
 67         curl_close($ch);    
 68     }
 69     public function menu(){
 70         $post='{
 71              "button":[{    
 72                             "type":"click",
 73                             "name":"今日歌曲",
 74                             "key":"V1001_TODAY_MUSIC"
 75                         },
 76                         {
 77                             "name":"菜单",
 78                             "sub_button":[{    
 79                                            "type":"view",
 80                                            "name":"下载说明书",
 81                                            "url":"http://www.baidu.com/"
 82                                         },
 83                                         {
 84                                            "type":"click",
 85                                            "name":"赞一下我们",
 86                                            "key":"V1001_GOOD",
 87                                         }]
 88                         }]
 89          }';
 90         $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->token();
 91         $ch=curl_init();
 92         curl_setopt($ch,CURLOPT_URL,$url);
 93         curl_setopt($ch,CURLOPT_POST,1);
 94         curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
 95         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 96         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 97         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 98         curl_exec($ch);
 99         curl_close($ch);
100     }
101     public function ticket(){
102         $url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->token();
103         $post='{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}';
104         $ch=curl_init();
105         curl_setopt($ch,CURLOPT_URL,$url);
106         curl_setopt($ch,CURLOPT_POST,1);
107         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
108         curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
109         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
110         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
111         $res=curl_exec($ch);
112         $jsonObj=json_decode($res);
113         return $jsonObj->ticket;
114         curl_close($ch);
115     }
116     public function qrCode(){
117         $ticket=UrlEncode($this->ticket());
118         $url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket;
119         $ch=curl_init();
120         curl_setopt($ch,CURLOPT_URL,$url);
121         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
122         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
123         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
124         curl_exec($ch);
125         curl_close($ch);
126     }
127     public function responseMsg(){
128         $postStr=$GLOBALS['HTTP_RAW_POST_DATA'];
129         if($postStr){
130             $postObj=$xml=simplexml_load_string($postStr);
131             $ToUserName=$postObj->ToUserName;
132             $FromUserName=$postObj->FromUserName;
133             $MsgType=$postObj->MsgType;
134             $Event=$postObj->Event;
135             $time=time();
136             if(strtolower($MsgType)=='event'&&strtolower($Event)=='subscribe'){
137                 $EventKey=$postObj->EventKey;    
138                 $arr=explode('_',$EventKey);
139                 $EventKey=$arr[1];
140                 $url="http://testbed02.flnettv.com/api3.0/helpespec?site=0&model=".$EventKey;
141                 $ch=curl_init();
142                 curl_setopt($ch,CURLOPT_URL,$url);
143                 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
144                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
145                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
146                 $jsonStr=curl_exec($ch);
147                 $jsonObj=json_decode($jsonStr);
148                 $model=$jsonObj->data->model;
149         
150                 $downloadurl=$jsonObj->data->downloadurl;
151                 $xml="<xml> 
152                 <ToUserName>%s</ToUserName> 
153                 <FromUserName>%s</FromUserName> 
154                 <CreateTime>%s</CreateTime> 
155                 <MsgType>text</MsgType> 
156             
157                 <Content>亲爱的夏普用户,您爱机的型号是:".$model.",请点击&lt;a href=&quot;$downloadurl &quot; &gt;下载说明书&lt;/a&gt; </Content>
158                 
159                 </xml>";
160                 $info=printf($xml,$FromUserName,$ToUserName,$time);
161                 echo $info;
162             }elseif(strtolower($MsgType)=='event'&&strtolower($Event)=='scan'){
163                 $EventKey=$postObj->EventKey;
164                 $url="http://testbed02.flnettv.com/api3.0/helpespec?site=0&model=".$EventKey;
165                 $ch=curl_init();
166                 curl_setopt($ch,CURLOPT_URL,$url);
167                 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
168                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
169                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
170                 $jsonStr=curl_exec($ch);
171                 $jsonObj=json_decode($jsonStr);
172                 $model=$jsonObj->data->model;
173                 
174                 $downloadurl=$jsonObj->data->downloadurl;
175                 $xml="<xml> 
176                 <ToUserName>%s</ToUserName> 
177                 <FromUserName>%s</FromUserName> 
178                 <CreateTime>%s</CreateTime> 
179                 <MsgType>text</MsgType> 
180             
181                 <Content>亲爱的夏普用户,您爱机的型号是:".$model.",请点击&lt;a href=&quot;$downloadurl &quot; &gt;下载说明书&lt;/a&gt; </Content>
182                 
183                 </xml>";
184                 $info=printf($xml,$FromUserName,$ToUserName,$time);
185                 echo $info;
186             }            
187         }
188     }
189 }
190 ?>

 

posted @ 2018-11-02 16:55  追梦鑫求  阅读(454)  评论(0)    收藏  举报