php微信公眾號(hào)開發(fā)之快遞查詢
更新時(shí)間:2018年10月20日 11:01:48 作者:dq_095
這篇文章主要為大家詳細(xì)介紹了php微信公眾號(hào)開發(fā)之快遞查詢功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了php微信公眾號(hào)開發(fā)之快遞查詢的具體代碼,供大家參考,具體內(nèi)容如下
快遞查詢
- 數(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- php微信公眾號(hào)開發(fā)模式詳解
- PHP實(shí)現(xiàn)微信公眾號(hào)驗(yàn)證Token的示例代碼
- php實(shí)現(xiàn)微信公眾號(hào)創(chuàng)建自定義菜單功能的實(shí)例代碼
- PHP實(shí)現(xiàn)的微信公眾號(hào)掃碼模擬登錄功能示例
- 微信公眾平臺(tái)開發(fā)教程③ PHP實(shí)現(xiàn)微信公眾號(hào)支付功能圖文詳解
- php微信公眾號(hào)開發(fā)之校園圖書館
- php微信公眾號(hào)開發(fā)之歡迎老朋友
- php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù)
- php微信公眾號(hào)開發(fā)之圖片回復(fù)
- 基于PHP的微信公眾號(hào)的開發(fā)流程詳解
相關(guān)文章
PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】
這篇文章主要介紹了PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫,結(jié)合實(shí)例形式分析了基于thinkPHP5.1框架使用ODBC連接SQL Server2008數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
Laravel框架實(shí)現(xiàn)利用中間件進(jìn)行操作日志記錄功能
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)利用中間件進(jìn)行操作日志記錄功能,結(jié)合實(shí)例形式分析了Laravel框架中間件的創(chuàng)建、引入以及使用中間件進(jìn)行操作日志記錄功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06
PHP實(shí)現(xiàn)抓取Google IP并自動(dòng)修改hosts文件
這篇文章主要介紹了PHP實(shí)現(xiàn)抓取Google IP并自動(dòng)修改hosts文件,本文方法可以實(shí)現(xiàn)免翻墻上google,小編親測(cè)可用,需要的朋友可以參考下2015-02-02

