1. 程式人生 > >微信公眾平臺,微信接入及消息回復

微信公眾平臺,微信接入及消息回復

llb file deb eat header host esc nat openid

 <?php
  /*
      方倍工作室 http://www.fangbei.org/
      CopyRight 2016 All Rights Reserved
  */
  header(‘Content-type:text‘);
  
  define("TOKEN", "weixin");
  $wechatObj = new wechatCallbackapiTest();
  if (!isset($_GET[‘echostr‘])) {
      $wechatObj->responseMsg();
  }else{
      $wechatObj->valid();
 
} class wechatCallbackapiTest { //驗證簽名 public function valid() { $echoStr = $_GET["echostr"]; $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array
($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ echo $echoStr; exit; } } //響應消息 public function responseMsg() {
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R \r\n".$postStr); $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //消息類型分離 switch ($RX_TYPE) { case "event": //事件 $result = $this->receiveEvent($postObj); break; case "text": //文本 $result = $this->receiveText($postObj); break; case "image": //圖片 $result = $this->receiveImage($postObj); break; case "location": //位置 $result = $this->receiveLocation($postObj); break; case "voice": //語音 $result = $this->receiveVoice($postObj); break; case "video": //視頻 case "shortvideo": $result = $this->receiveVideo($postObj); break; case "link": //鏈接 $result = $this->receiveLink($postObj); break; default: $result = "unknown msg type: ".$RX_TYPE; break; } $this->logger("T \r\n".$result); echo $result; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "歡迎關註方倍工作室 \n請回復以下關鍵字:文本 表情 單圖文 多圖文 音樂\n請按住說話 或 點擊 + 再分別發送以下內容:語音 圖片 小視頻 我的收藏 位置"; break; case "unsubscribe": $content = "取消關註"; break; default: $content = "receive a new event: ".$object->Event; break; } if(is_array($content)){ $result = $this->transmitNews($object, $content); }else{ $result = $this->transmitText($object, $content); } return $result; } //接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); if (strstr($keyword, "文本")){ //回復文本 $content = "這是個文本消息"; }else if (strstr($keyword, "表情")){ $content = "微笑:/::)\n乒乓:/:oo\n中國:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335); }else if (strstr($keyword, "單圖文")){ //回復圖文消息 $content = array(); $content[] = array("Title"=>"單圖文標題", "Description"=>"單圖文內容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); }else if (strstr($keyword, "圖文") || strstr($keyword, "多圖文")){ $content = array(); $content[] = array("Title"=>"多圖文1標題", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多圖文2標題", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多圖文3標題", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); }else if (strstr($keyword, "音樂")){ //回復音樂消息 $content = array(); $content = array("Title"=>"最炫民族風", "Description"=>"歌手:鳳凰傳奇", "MusicUrl"=>"http://mascot-music.stor.sinaapp.com/zxmzf.mp3", "HQMusicUrl"=>"http://mascot-music.stor.sinaapp.com/zxmzf.mp3"); }else{ $content = date("Y-m-d H:i:s",time())."\nOpenID:".$object->FromUserName."\n技術支持 方倍工作室"; } if(is_array($content)){ if (isset($content[0])){ $result = $this->transmitNews($object, $content); }else if (isset($content[‘MusicUrl‘])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } //接收圖片消息 private function receiveImage($object) { $content = array("MediaId"=>$object->MediaId); $result = $this->transmitImage($object, $content); return $result; } //接收位置消息 private function receiveLocation($object) { $content = "你發送的是位置,經度為:".$object->Location_Y.";緯度為:".$object->Location_X.";縮放級別為:".$object->Scale.";位置為:".$object->Label; $result = $this->transmitText($object, $content); return $result; } //接收語音消息 private function receiveVoice($object) { if (isset($object->Recognition) && !empty($object->Recognition)){ $content = "你剛才說的是:".$object->Recognition; $result = $this->transmitText($object, $content); }else{ $content = array("MediaId"=>$object->MediaId); $result = $this->transmitVoice($object, $content); } return $result; } //接收視頻消息 private function receiveVideo($object) { $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>""); $result = $this->transmitVideo($object, $content); return $result; } //接收鏈接消息 private function receiveLink($object) { $content = "你發送的是鏈接,標題為:".$object->Title.";內容為:".$object->Description.";鏈接地址為:".$object->Url; $result = $this->transmitText($object, $content); return $result; } //回復文本消息 private function transmitText($object, $content) { if (!isset($content) || empty($content)){ return ""; } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } //回復圖文消息 private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return ""; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]); } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> $item_str </Articles> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } //回復音樂消息 private function transmitMusic($object, $musicArray) { if(!is_array($musicArray)){ return ""; } $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回復圖片消息 private function transmitImage($object, $imageArray) { $itemTpl = "<Image> <MediaId><![CDATA[%s]]></MediaId> </Image>"; $item_str = sprintf($itemTpl, $imageArray[‘MediaId‘]); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回復語音消息 private function transmitVoice($object, $voiceArray) { $itemTpl = "<Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice>"; $item_str = sprintf($itemTpl, $voiceArray[‘MediaId‘]); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回復視頻消息 private function transmitVideo($object, $videoArray) { $itemTpl = "<Video> <MediaId><![CDATA[%s]]></MediaId> <ThumbMediaId><![CDATA[%s]]></ThumbMediaId> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> </Video>"; $item_str = sprintf($itemTpl, $videoArray[‘MediaId‘], $videoArray[‘ThumbMediaId‘], $videoArray[‘Title‘], $videoArray[‘Description‘]); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[video]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //日誌記錄 private function logger($log_content) { if(isset($_SERVER[‘HTTP_APPNAME‘])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER[‘REMOTE_ADDR‘] != "127.0.0.1"){ //LOCAL $max_size = 1000000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date(‘Y-m-d H:i:s‘)." ".$log_content."\r\n", FILE_APPEND); } } } ?>

微信公眾平臺,微信接入及消息回復