微信邀請卡的開發

  1 <?php
  2 /**
  3   * wechat php test
  4   */
  5 
  6 //define your token
  7 define("TOKEN", "weixin");
  8 $wechatObj = new wechatCallbackapiTest();
  9 //驗證服務器和公衆平臺是否鏈接成功
 10 //在服務器和公衆平臺驗證成功以後,把$wechatObj->valid()註釋掉
 11 // $wechatObj->valid();
 12 echo $wechatObj->responseMsg();
 13 
 14 class wechatCallbackapiTest
 15 {
 16     public function valid()
 17     {
 18         $echoStr = $_GET["echostr"];
 19 
 20         //valid signature , option
 21         if($this->checkSignature()){
 22             echo $echoStr;
 23             exit;
 24         }
 25     }
 26     //輸出公衆平臺返回給用戶的信息
 27     public function responseMsg()
 28     {
 29         //get post data, May be due to the different environments
 30         //至關於$_POST
 31         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 32 
 33           //extract post data
 34         if (!empty($postStr)){
 35                 /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
 36                    the best way is to check the validity of xml by yourself */
 37                 //只解析XML數據的主體部分,防止xxe攻擊
 38                 libxml_disable_entity_loader(true);
 39                 //解析XML數據
 40                   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
 41                 //獲取手機用戶的OPenID
 42                 $fromUsername = $postObj->FromUserName;
 43                 //開發者微信號
 44                 $toUsername = $postObj->ToUserName;
 45                 //發送文本信息的關鍵字
 46                 $keyword = trim($postObj->Content);
 47                 //發送消息的類型
 48                 $type = $postObj->MsgType;
 49 
 50                 $time = time();
 51                 //發送文本信息的字符串模板
 52                 $textTpl = "<xml>
 53                             <ToUserName><![CDATA[%s]]></ToUserName>
 54                             <FromUserName><![CDATA[%s]]></FromUserName>
 55                             <CreateTime>%s</CreateTime>
 56                             <MsgType><![CDATA[%s]]></MsgType>
 57                             <Content><![CDATA[%s]]></Content>
 58                             <FuncFlag>0</FuncFlag>
 59                             </xml>";
 60                 //音樂消息的模板
 61                 $musicTpl = "<xml>
 62                             <ToUserName><![CDATA[%s]]></ToUserName>
 63                             <FromUserName><![CDATA[%s]]></FromUserName>
 64                             <CreateTime>%s</CreateTime>
 65                             <MsgType><![CDATA[%s]]></MsgType>
 66                             <Music>
 67                             <Title><![CDATA[%s]]></Title>
 68                             <Description><![CDATA[%s]]></Description>
 69                             <MusicUrl><![CDATA[%s]]></MusicUrl>
 70                             <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
 71                             
 72                             </Music>
 73                             </xml>";
 74                 //發送圖文消息
 75                 $newsTpl="<xml>
 76                             <ToUserName><![CDATA[%s]]></ToUserName>
 77                             <FromUserName><![CDATA[%s]]></FromUserName>
 78                             <CreateTime>%s</CreateTime>
 79                             <MsgType><![CDATA[%s]]></MsgType>
 80                             <ArticleCount>%s</ArticleCount>
 81                             <Articles>
 82                             %s
 83                             </Articles>
 84                             </xml> ";                           
 85                 if($type == 'text'){
 86                     if(!empty( $keyword ))
 87                     {
 88                         // if($keyword == '音樂'){
 89                         //     $msgType = "music";
 90                         //     $title   = "See You Again";
 91                         //     $description='速度與激情7 原聲大碟';
 92                         //     $music_url='http://www.yyzljg.com/wechat/music.mp3';
 93                         //     $high_url='http://www.yyzljg.com/wechat/music.mp3';
 94                         //     //格式化字符串
 95                         //     $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$description,$music_url,$high_url);
 96                         //     echo $resultStr;
 97                         // }
 98                         if($keyword=='音樂'){
 99                                 $msgType='music';
100                                 $title='冰雪奇緣主題曲';
101                                 $description='原聲大碟';
102                                 $music_url='http://www.yyzljg.com/wechat/music.mp3';
103                                 $high_url='http://www.yyzljg.com/wechat/music.mp3';
104                                 //格式化字符串
105                                 $resultStr = sprintf($musicTpl, $fromUsername, $toUsername, $time, $msgType,$title,$description,$music_url,$high_url);
106                                 echo $resultStr;
107                         }
108                         if($keyword == '單圖文'){
109                             $msgType='news';
110                             $counts=1;
111                             $contentStr="<item>
112                             <Title><![CDATA[你們一塊兒學習微信開發]]></Title>
113                             <Description><![CDATA[願天天的你都是開心的!]]></Description>
114                             <PicUrl><![CDATA[http://ibeliveone.applinzi.com/1.jpg]]></PicUrl>
115                             <Url><![CDATA[http://ibeliveone.applinzi.com/1.jpg]]></Url>
116                             </item>";
117 
118                             //格式化字符串
119                             $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
120                             echo $resultStr;
121                         }
122                         if($keyword == '多圖文'){
123                             $msgType='news';
124                             $counts=4;
125                             $contentStr='';
126                             for($i=1;$i<=4;$i++){
127                                 $contentStr.="<item>
128                                 <Title><![CDATA[你們一塊兒學習微信開發]]></Title>
129                                 <Description><![CDATA[願天天的你都是開心的!]]></Description>
130                                 <PicUrl><![CDATA[http://ibeliveone.applinzi.com/{$i}.jpg]]></PicUrl>
131                                 <Url><![CDATA[http://ibeliveone.applinzi.com/{$i}.jpg]]></Url>
132                                 </item>";
133                             }
134                             //格式化字符串
135                             $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType,$counts,$contentStr);
136                             echo $resultStr;
137                         } 
138                         
139                         //定義發送消息的類型
140                         $msgType = "text";
141                         //公衆平臺發送給用戶的信息
142                         $contentStr = "歡迎關注個人微信公衆平臺!";
143                         //格式化字符串 
144                         $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
145                         echo $resultStr;die;               
146                     
147                     
148                     }else{
149                         echo "Input something...";
150                     }
151                 }elseif($type == 'image'){
152                     //定義發送消息的類型
153                     $msgType = "text";
154                     //公衆平臺發送給用戶的信息
155                     $contentStr = "客官,您發送的美女好漂亮啊!";
156                     //格式化字符串 
157                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
158                     echo $resultStr; 
159                     die;  
160                 }elseif($type == 'voice'){
161                     //定義發送消息的類型
162                     $msgType = "text";
163                     //公衆平臺發送給用戶的信息
164                     $contentStr = "客官,您的聲音好動聽啊!";
165                     //格式化字符串 
166                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
167                     echo $resultStr;
168                     die;
169                 }elseif($type == 'video'){
170                     //定義發送消息的類型
171                     $msgType = "text";
172                     //公衆平臺發送給用戶的信息
173                     $contentStr = "客官,您分享的片片不是高清滴呦!";
174                     //格式化字符串 
175                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
176                     echo $resultStr;
177                     die;
178                 }else{
179                     //定義發送消息的類型
180                     $msgType = "text";
181                     //公衆平臺發送給用戶的信息
182                     $contentStr = "客官,我書讀的少,不知道你想要什麼服務,回覆關鍵字有驚喜:'音樂','單圖文','多圖文'";
183                     //格式化字符串 
184                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
185                     echo $resultStr;
186                     die;
187                 }                      
188                 
189 
190         }else {
191             echo "客官,我書讀的少,不知道你想要什麼服務,回覆關鍵字有驚喜:'音樂','單圖文','多圖文'";
192             exit;
193         }
194     }
195         
196     private function checkSignature()
197     {
198         // you must define TOKEN by yourself
199         if (!defined("TOKEN")) {
200             throw new Exception('TOKEN is not defined!');
201         }
202         
203         $signature = $_GET["signature"];
204         $timestamp = $_GET["timestamp"];
205         $nonce = $_GET["nonce"];
206                 
207         $token = TOKEN;
208         $tmpArr = array($token, $timestamp, $nonce);
209         // use SORT_STRING rule
210         sort($tmpArr, SORT_STRING);
211         $tmpStr = implode( $tmpArr );
212         $tmpStr = sha1( $tmpStr );
213         
214         if( $tmpStr == $signature ){
215             return true;
216         }else{
217             return false;
218         }
219     }
220 }
221 
222 ?>
相關文章
相關標籤/搜索