您好,登錄后才能下訂單哦!
使用php怎么實(shí)現(xiàn)快遞查詢功能?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
快遞查詢
數(shù)組用法
foreach
查詢接口是:愛快遞:https://www.aikuaidi.cn/api/
核心代碼如下:
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $type = $postObj->MsgType; $customrevent = $postObj->Event; $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; $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>"; switch ($type) { case "text"; $status=array('0'=>'查詢出錯(cuò)','1'=>'暫無記錄','2'=>'在途中','3'=>'派送中','4'=>'已簽收','5'=>'拒收','6'=>'疑難件','7'=>'退回');//構(gòu)建快遞狀態(tài)數(shù)組 $kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$keyword}&id=shentong&show=xml";//快遞地址 $kuaidistr=file_get_contents($kuaidiurl);//讀入文件 $kuaidiobj=simplexml_load_string($kuaidistr);//xml解析 $kuaidistatus = $kuaidiobj->Status;//獲取快遞狀態(tài) $kuaistr=strval($kuaidistatus);//對(duì)象轉(zhuǎn)換為字符串 $contentStr0 =$status[$kuaistr];//根據(jù)數(shù)組返回 foreach ($kuaidiobj->Data->Order as $a) { foreach ($a->Time as $b) { foreach ($a->Content as $c) { $m.="{$b}{$c}";} } } //遍歷獲取快遞時(shí)間和事件 $contentStr="你的快遞單號(hào){$keyword}{$contentStr0}{$m}"; break; default; $contentStr ="此項(xiàng)功能尚未開發(fā)"; } $msgType="text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;
index.php整體代碼如下:
<?php /** * wechat php test */ //define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); //$wechatObj->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)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $type = $postObj->MsgType; $customrevent = $postObj->Event; $latitude = $postObj->Location_X; $longitude = $postObj->Location_Y; $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>"; switch ($type) { case "text"; $status=array('0'=>'查詢出錯(cuò)','1'=>'暫無記錄','2'=>'在途中','3'=>'派送中','4'=>'已簽收','5'=>'拒收','6'=>'疑難件','7'=>'退回');//構(gòu)建快遞狀態(tài)數(shù)組 $kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$keyword}&id=shentong&show=xml";//快遞地址 $kuaidistr=file_get_contents($kuaidiurl);//讀入文件 $kuaidiobj=simplexml_load_string($kuaidistr);//xml解析 $kuaidistatus = $kuaidiobj->Status;//獲取快遞狀態(tài) $kuaistr=strval($kuaidistatus);//對(duì)象轉(zhuǎn)換為字符串 $contentStr0 =$status[$kuaistr];//根據(jù)數(shù)組返回 foreach ($kuaidiobj->Data->Order as $a) { foreach ($a->Time as $b) { foreach ($a->Content as $c) { $m.="{$b}{$c}";} } } //遍歷獲取快遞時(shí)間和事件 $contentStr="你的快遞單號(hào){$keyword}{$contentStr0}{$m}"; break; default; $contentStr ="此項(xiàng)功能尚未開發(fā)"; } $msgType="text"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }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; } } } ?>
看完上述內(nèi)容,你們掌握使用php怎么實(shí)現(xiàn)快遞查詢功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。