php微信公众号开发之快递查询
作者:dq_095 时间:2023-11-11 03:30:36
本文实例为大家分享了php微信公众号开发之快递查询的具体代码,供大家参考,具体内容如下
快递查询
数组用法
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'=>'查询出错','1'=>'暂无记录','2'=>'在途中','3'=>'派送中','4'=>'已签收','5'=>'拒收','6'=>'疑难件','7'=>'退回');//构建快递状态数组
$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;//获取快递状态
$kuaistr=strval($kuaidistatus);//对象转换为字符串
$contentStr0 =$status[$kuaistr];//根据数组返回
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 ="此项功能尚未开发";
}
$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'=>'查询出错','1'=>'暂无记录','2'=>'在途中','3'=>'派送中','4'=>'已签收','5'=>'拒收','6'=>'疑难件','7'=>'退回');//构建快递状态数组
$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;//获取快递状态
$kuaistr=strval($kuaidistatus);//对象转换为字符串
$contentStr0 =$status[$kuaistr];//根据数组返回
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 ="此项功能尚未开发";
}
$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;
}
}
}
?>
来源:https://blog.csdn.net/qq_37968920/article/details/82051795
标签:php,微信,快递查询
0
投稿
猜你喜欢
python把一个字符串切开的实例方法
2022-07-10 21:52:17
数据库分页大全(mssql,mysql,oracle)
2010-10-25 20:02:00
小记一次mysql主从配置解决方案
2024-01-12 18:28:42
windows下python安装paramiko模块和pycrypto模块(简单三步)
2021-11-29 01:18:40
Python实现连接MySql数据库及增删改查操作详解
2024-01-14 10:40:36
css实现简单圆角效果
2008-11-27 13:11:00
sqlserver数据库优化解析(图文剖析)
2024-01-17 08:55:37
在Python的Flask中使用WTForms表单框架的基础教程
2023-05-10 05:20:08
TypeScript 学习笔记之基本类型
2024-06-07 15:56:29
python实现简易的学生信息管理系统
2021-08-24 12:07:09
python一秒搭建FTP服务器
2021-03-04 01:44:30
通过PHP current函数获取未知字符键名数组第一个元素的值
2023-11-15 04:52:07
python中的代码运行时间获取方式
2023-11-19 22:57:02
使用pyecharts无法import Bar的解决方案
2021-04-02 21:31:15
JS简单实现无缝滚动效果实例
2024-04-16 09:34:52
Python数学建模PuLP库线性规划进阶基于字典详解
2022-03-11 18:04:04
解决Jupyter notebook更换主题工具栏被隐藏及添加目录生成插件问题
2023-11-08 21:32:55
详解Mysql数据库平滑扩容解决高并发和大数据量问题
2024-01-19 12:52:13
python实现小世界网络生成
2022-07-22 07:54:46
关于TypeScript模块导入的那些事
2024-06-07 15:57:46