php通过快递单号查询快递公司
时间: 2023-09-01 12:14:03 浏览: 104
要通过快递单号查询快递公司,可以通过调用快递鸟等第三方快递查询平台的API接口来实现。以下是一个简单的示例代码:
```php
// 发送请求的URL
$url = 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
// 请求参数
$params = array(
'RequestData' => json_encode(array(
'LogisticCode' => '快递单号',
'ShipperCode' => '', // 快递公司编码,可以为空
'OrderCode' => ''
)),
'EBusinessID' => '快递鸟分配的EBusinessID',
'RequestType' => '2002',
'DataSign' => strtoupper(md5('请求参数' . '快递鸟分配的API key'))
);
// 发送POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
// 解析JSON响应结果
$data = json_decode($result, true);
if ($data['Success'] && $data['State'] == 3) {
$shipperCode = $data['Shippers'][0]['ShipperCode'];
echo '快递公司编码:' . $shipperCode;
} else {
echo '查询快递公司失败!';
}
```
其中,请求参数中的`LogisticCode`为快递单号,`快递鸟分配的EBusinessID`和`快递鸟分配的API key`需要向快递鸟申请获取,`$data['Shippers'][0]['ShipperCode']`为获取的快递公司编码。其他快递查询平台的API接口使用方法类似,需要根据具体的接口文档进行调整。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)