微信公眾號開發之簡訊自動回複php代碼_php執行個體

來源:互聯網
上載者:User
本文執行個體為大家分享了php簡訊自動回複 別代碼,供大家參考,具體內容如下

1.PHP範例程式碼下載
下載地址1:http://xiazai.php.net/201608/yuanma/phpwx(php.net).rar
下載地址2:https://mp.weixin.qq.com/wiki/home/index.html(開始開發-》接入指南-》PHP範例程式碼下載)

2.wx_sample.php初始代碼

valid();class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,  the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true);  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "%s0";  if(!empty( $keyword )) {  $msgType = "text";  $contentStr = "Welcome to wechat world!";  $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  echo $resultStr; }else{  echo "Input something..."; } }else { echo ""; exit; } }  private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); }  $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];  $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr );  if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>

3.調用回複資訊方法
在wx_sample.php檔案中注釋掉$wechatObj->valid();,在其下增加一句“$wechatObj->responseMsg();”。

valid();//介面驗證$wechatObj->responseMsg();//調用回複訊息方法class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,  the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true);  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "%s0";  if(!empty( $keyword )) {  $msgType = "text";  $contentStr = "Welcome to wechat world!";  $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  echo $resultStr; }else{  echo "Input something..."; } }else { echo ""; exit; } }  private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); }  $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];  $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr );  if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>

4.關鍵詞自動回複和關注回複
$keyword儲存著使用者端發來的文本資訊。
官方開發人員文檔:https://mp.weixin.qq.com/wiki/home/index.html(訊息管理-》接收訊息-接收事件推送-》1.關注/取消追蹤事件)

關注事件與一般的簡訊有兩處不同,一是MsgType值是event,二是增加了Event值是subscribe。由於官方文檔(最初的wx_sample.php)沒有提取這個參數,需要我們自己提取。在程式中增加兩個變數$msgType和$event。

valid();//介面驗證$wechatObj->responseMsg();//調用回複訊息方法class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,  the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true);  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $msgType = $postObj->MsgType;//訊息類型 $event = $postObj->Event;//時間類型,subscribe(訂閱)、unsubscribe(取消訂閱) $textTpl = "%s0";    switch($msgType){  case "event":  if($event=="subscribe"){  $contentStr = "Hi,歡迎關注海仙日用百貨!"."\n"."回複數字'1',瞭解店鋪地址."."\n"."回複數字'2',瞭解商品種類.";  }   break;  case "text":  switch($keyword){  case "1":  $contentStr = "店鋪地址:"."\n"."杭州市江幹艮山西路233號新東升市場地下室第一排.";   break;  case "2":  $contentStr = "商品種類:"."\n"."杯子、碗、棉簽、水桶、垃圾桶、洗碗巾(刷)、拖把、掃把、"   ."衣架、粘鉤、牙籤、垃圾袋、保鮮袋(膜)、剪刀、水果刀、飯盒等.";  break;  default:  $contentStr = "對不起,你的內容我會稍後回複";  }  break; } $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else { echo ""; exit; } }  private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); }  $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];  $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr );  if( $tmpStr == $signature ){ return true; }else{ return false; } }}?>

以上就是公眾號開發之簡訊自動回複php代碼_php執行個體的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.