PHP-微信開發之圖靈機器人--天氣介面呼叫
阿新 • • 發佈:2019-01-12
微信開發中,圖靈機器人回覆、天氣、翻譯、美食、地圖、二維碼功能,隨處可見。大小網站、APP都在運用這項技術在專案中。今天簡單介紹一下圖靈機器人回覆,天氣介面呼叫、翻譯介面,希望對朋友們有用。
效果案例:
思路很簡單,首先,token要一致,然後在瀏覽器上跑起來, 回到微信平臺配置後進行提交,顯示配置成功,則OK!配置失敗,檢查token和程式碼規範,如果還不行,百度上有很多解決辦法,不過有的沒有用。可以試一試不用框架可不可以執行。
下面不多說,上程式碼:
<?php /** * wechat php test */ //define your token define("TOKEN","token可以隨意些");//定義常量token值,與微信中的值進行對比 $wechatObj = new wechatCallbackapiTest();//例項化下面的類 //判斷微信傳過來的值是否存在 if(!isset($_GET['echostr'])){ //如果微信傳過來的值存在執行下面方法 $wechatObj->responseMsg(); }else{ //不存在執行下面方法 $wechatObj->valid(); } //$wechatObj->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 = file_get_contents('php://input'); //全域性變數的資訊 可以接收 //extract post data if (!empty($postStr)){ //如果$postStr不為空 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch($RX_TYPE) { case "text": $resultStr = $this->handleText($postObj); break; case "event": $resultStr = $this->handleEvent($postObj); break; default: $resultStr = "Unknow msg type: ".$RX_TYPE; break; } echo $resultStr; }else { echo ""; exit; } } //繫結圖靈機器人,返回資料轉化為微信使用的格式 public function handleText($postObj) { $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty($keyword )) { //主要內容--機器人---天氣介面--- $msgType = "text"; if($keyword=="部落格"){ $contentStr = "YFeng888的部落格地址是:https://blog.csdn.net/weixin_42980713"; }else if($keyword=="天氣"){ //建立函式呼叫圖靈機器人介面 $url = "http://apis.haoservice.com/weather?cityname=北京&key=99ae8b89d4a14c0881c2ac64c831cdd6"; $send_info = file_get_contents($url); $c = json_decode($send_info,true); $contentStr = "【".$c['result']['today']['city']."天氣預報】\n"." ".$c['result']['today']['date_y']."時釋出"."\n\n實時天氣\n".$c['result']['today']['weather'].$c['result']['today']['temperature'].$c['result']['today']['wind']."\n\n溫馨提示:".$c['result']['today']['dressing_advice']."\n\n明天\n".$c['result']['future'][1]['temperature']." \n\n後天".$c['result']['future'][2]['temperature']."\n"; }elseif($keyword=="經典語錄"){ //建立函式呼叫圖靈機器人介面 $url="http://apis.haoservice.com/lifeservice/JingDianYulu/getTypes?key=你自己的key"; $send_info = file_get_contents($url); $json = json_decode($send_info,true); $contentStr=$json['result']; }else{ //翻譯介面 $url = "http://apis.haoservice.com/efficient/robot?info=$keyword&address=&key=你自己的key"; $send_info = file_get_contents($url); $json = json_decode($send_info,true); $contentStr =$json['result']['text']; file_put_contents("haoyu.txt",$contenStr);//寫入日誌到haoyu.txt檔案內 } // $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } } public function handleEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "感謝您關注【最帥男人幫】"."\n"."微訊號:shuaigege"."\n"."卓越錦繡,名城蘇州,我們為您提供蘇州本地生活指南,蘇州相關資訊查詢,做最好的蘇州微信平臺。"."\n"."目前平臺功能如下:"."\n"."【1】 查天氣,如輸入:蘇州天氣"."\n"."【2】 查公交,如輸入:蘇州公交178"."\n"."【3】 翻譯,如輸入:翻譯I love you"."\n"."【4】 蘇州資訊查詢,如輸入:蘇州觀前街"."\n"."更多內容,敬請期待..."; break; default : $contentStr = "Unknow Event: ".$object->Event; break; } $resultStr = $this->responseText($object, $contentStr); return $resultStr; } public function responseText($object, $content, $flag=0) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%d</FuncFlag> </xml>"; $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag); return $resultStr; } 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; } } } ?>
ok,親測可行!