一頁代碼實現微信基本回複和點擊事件功能,部署上去sae或者bae,妥妥的基本免費的伺服器
不懂代碼都基本每個人都可以做自己的微信公眾號了
valid();}else{ //回複訊息 $wechatObj->responseMsg();}class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } //回複訊息 public function responseMsg(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "text": $resultStr = $this->receiveText($postObj); break; case "image": $resultStr = $this->receiveImage($postObj); break; case "location": $resultStr = $this->receiveLocation($postObj); break; case "voice": $resultStr = $this->receiveVoice($postObj); break; case "video": $resultStr = $this->receiveVideo($postObj); break; case "link": $resultStr = $this->receiveLink($postObj); break; case "event": $resultStr = $this->receiveEvent($postObj); break; default: $resultStr = "unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; }} //接收簡訊 private function receiveText($object) { $keyword = trim($object->Content); $url = "http://api100.duapp.com/movie/?appkey=DIY_miaomiao&name=".$keyword; $output = file_get_contents($url,$keyword); $contentStr = json_decode($output, true); if (is_array($contentStr)){ $resultStr = $this->transmitNews($object, $contentStr); }else{ $resultStr = $this->transmitText($object, $contentStr); } return $resultStr; } //接收事件,關注等 private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "你關注了我"; //關注後回複內容 break; case "unsubscribe": $contentStr = ""; break; case "CLICK": $contentStr = $this->receiveClick($object); //點擊事件 break; default: $contentStr = "receive a new event: ".$object->Event; break; } return $contentStr; } //接收圖片 private function receiveImage($object) { $contentStr = "你發送的是圖片,地址為:".$object->PicUrl; $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } //接收語音 private function receiveVoice($object) { $contentStr = "你發送的是語音,媒體ID為:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } //接收視頻 private function receiveVideo($object) { $contentStr = "你發送的是視頻,媒體ID為:".$object->MediaId; $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } //位置訊息 private function receiveLocation($object) { $contentStr = "你發送的是位置,緯度為:".$object->Location_X.";經度為:".$object->Location_Y.";縮放層級為:".$object->Scale.";位置為:".$object->Label; $resultStr = $this->transmitText($object, $contentStr); return $resultStr; } //連結訊息 private function receiveLink($object) { $contentStr = "你發送的是連結,標題為:".$object->Title.";內容為:".$object->Description.";連結地址為:".$object->Url; $resultStr = $this->transmitText($object, $contentStr); return $resultStr; }
//點擊菜單訊息 private function receiveClick($object) { switch ($object->EventKey) { case "1": $contentStr = "貓咪醬個性DIY服裝, 我們專業定製個性【班服,情侶裝,親子裝等,有長短T恤,衛衣,長短褲】 來圖印製即可,給你溫馨可愛的TA, 有事可直接留言微信"; break; case "2": $contentStr = "你點擊了菜單: ".$object->EventKey; break; case "3": $contentStr = "是傻逼"; break; default: $contentStr = "你點擊了菜單: ".$object->EventKey; break; } //兩種回複 if (is_array($contentStr)){ $resultStr = $this->transmitNews($object, $contentStr); }else{ $resultStr = $this->transmitText($object, $contentStr); } return $resultStr; } //回複簡訊 private function transmitText($object, $content) { $textTpl = " %s "; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $resultStr; }
//回複圖文 private function transmitNews($object, $arr_item) { if(!is_array($arr_item)) return;
$itemTpl = " "; $item_str = ""; foreach ($arr_item as $item) $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = " %s %s $item_str ";
$resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item)); return $resultStr; } //音樂訊息 private function transmitMusic($object, $musicArray, $flag = 0) { $itemTpl = " ";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$textTpl = " %s $item_str %d ";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag); return $resultStr; } } ?>