$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'=>'查詢出錯','1'=>'暫無記錄','2'=>'在途中','3'=>'派送中','4'=>'已簽收','5'=>'拒收','6'=>'疑難件','7'=>'退回');//構(gòu)建快遞狀態(tài)數(shù)組
$kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9order={$keyword}id=shentongshow=xml";//快遞地址
$kuaidistr=file_get_contents($kuaidiurl);//讀入文件
$kuaidiobj=simplexml_load_string($kuaidistr);//xml解析
$kuaidistatus = $kuaidiobj->Status;//獲取快遞狀態(tài)
$kuaistr=strval($kuaidistatus);//對象轉(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}";}
}
}
//遍歷獲取快遞時間和事件
$contentStr="你的快遞單號{$keyword}{$contentStr0}{$m}";
break;
default;
$contentStr ="此項功能尚未開發(fā)";
}
$msgType="text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
?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'=>'查詢出錯','1'=>'暫無記錄','2'=>'在途中','3'=>'派送中','4'=>'已簽收','5'=>'拒收','6'=>'疑難件','7'=>'退回');//構(gòu)建快遞狀態(tài)數(shù)組
$kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9order={$keyword}id=shentongshow=xml";//快遞地址
$kuaidistr=file_get_contents($kuaidiurl);//讀入文件
$kuaidiobj=simplexml_load_string($kuaidistr);//xml解析
$kuaidistatus = $kuaidiobj->Status;//獲取快遞狀態(tài)
$kuaistr=strval($kuaidistatus);//對象轉(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}";}
}
}
//遍歷獲取快遞時間和事件
$contentStr="你的快遞單號{$keyword}{$contentStr0}{$m}";
break;
default;
$contentStr ="此項功能尚未開發(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;
}
}
}
?>