在博文小黃雞simsimi非官方API被封了,轉用小i機器人吧(解決方法)提到現在的韓國小黃雞被封了,會出現api接入錯誤:Unauthorized access!. In this program(site, app), the SimSimi API is being used illegally. Please contact us. http://developer.simsimi.com。並建議大家轉國內小i機器人,不過沒過幾天也出現查封的問題,出現403錯誤,下面的內容轉 發自Kaedeen's Blog,牛人解決了這個問題,特此轉發。
以下內容為轉發,如果你要轉發,請注明鏈接:http://www.kaedeen.com/xiaoi-wap-source-code/。
這個依然是文字版的,而且不支持語音,是通過抓包分析小i機器人wap版的獲取。可以通過掃描下面的二維碼或者直接添加微信號xiaoibot進行關注思密達。
但是最近小i wap版的demo可能發現有人盜用了,所以把“機器人”封了,如果用之前的方法,會出現403 forbidden的錯誤,所以我又重新弄了一下,加上了一些瀏覽器信息,這樣就不會封了~
之前不想公布來着,但是沒有但是……共享才是王道,大家共同進步。
上代碼:
/** * *作者:Kaedeen *來源:http://www.kaedeen.com/ *日期:2013.1.7 *修改:2013.1.15 14號被封掉,錯誤是403,特此修改 * **/ //define your token define("TOKEN", "kaede"); $wxObj = new weixinCallbackApi(); //$wxObj->valid(); //第一次驗證時使用 $wxObj->responseMsg(); class weixinCallbackApi { 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)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $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"; $contentStr = ""; $picnews = ""; //新關注我的用戶 if($keyword=='Hello2BizUser') { $contentStr=$this->welcome($toUsername); } $contentStr = $this->get_wap_xiaoi($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something…"; } }else { echo ""; 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; } } //調用小i機器人wap版 public function get_wap_xiaoi($key){ $post_data = array( 'requestContent='.$key, ); $post_data = implode('&',$post_data); /***old method $url='http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action'; $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_USERAGENT,"Opera/9.60"); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); ob_start(); curl_exec($ch); $result = ob_get_contents() ; ob_end_clean(); */ $url="http://nlp.xiaoi.com/robot/demo/wap/"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $content = curl_exec($ch); curl_close($ch); //echo $content; list($header, $body) = explode("\r\n\r\n", $content); preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches); $cookie = $matches[1]; $ch = curl_init( ); curl_setopt( $ch, CURLOPT_REFERER, "http://nlp.xiaoi.com/robot/demo/wap/" ); curl_setopt( $ch, CURLOPT_HEADER, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" ); curl_setopt( $ch, CURLOPT_URL, "http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action" ); curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data ); curl_setopt($ch, CURLOPT_COOKIE, $cookie); $content = curl_exec($ch); curl_close($ch); sae_log($content); $preg = '/<\/span>(.*)<\/p>/iUs'; preg_match_all($preg,$content,$match); $response_msg=$match[0][0]; $preg = "/<\/?[^>]+>/i"; $response_msg=preg_replace($preg,'',$response_msg); if("hello,how are you"==$response_msg||"how do you do"==$response_msg) { $response_msg="小i機器人歡迎您,作者主頁地址:www.kaedeen.com。小i機器人不斷學習中,歡迎各種調戲…/:,@-D";//歡迎語 } $response_msg=trim($response_msg); return $response_msg; } //問候語 public function welcome($toUsername) { if($toUsername=="gh_48776be7ef17"){//微信原始id return "小i機器人歡迎您,作者主頁地址:www.kaedeen.com。小i機器人不斷學習中,歡迎各種調戲…/:,@-D";//歡迎語 } } } //sae打日志封裝函數 function sae_log($msg) { sae_set_display_errors(false);//關閉信息輸出 if (is_array($msg)) { $msg = implode(",", $msg); } sae_debug("[BEGIN]".$msg."[END]");//記錄日志 sae_set_display_errors(true);//記錄日志后再打開信息輸出,否則會阻止正常的錯誤信息的顯示 }
注明:因為博文的博主將代碼放在sina sae上,所以代碼中的sae_debug是平台上的調試方法,去掉即可在其他平台運行,enjoy~感謝Kaedeen~