注册快递100账号获取customer和key,构造包含com、num等信息的param参数,通过MD5加密生成sign签名,使用curl发送POST请求至https://poll.kuaidi100.com/poll/query.do,接收并解析返回的物流数据,实现PHP物流查询。

要实现PHP调用第三方物流API(如快递100)查询订单,核心步骤包括:注册账号获取Key、选择合适的接口类型(公开或企业版)、构造请求参数、发送HTTP请求并解析返回结果。下面以快递100为例,详细介绍具体实现方法。
使用快递100等平台前,需先注册开发者账号:
快递100推荐使用POST方式提交JSON数据到指定接口地址。常用接口为:
https://poll.kuaidi100.com/poll/query.do请求头需设置 Content-Type: application/json,请求体包含以下关键字段:
立即学习“PHP免费学习笔记(深入)”;
其中 param 是一个JSON字符串,结构如下:
{
"com": "yunda", // 快递公司编码,可为空由系统自动识别
"num": "123456789", // 快递单号
"from": "", // 发货地(可选)
"to": "", // 收货地(可选)
"resultv2": "1" // 是否开启增值功能(1=开启)
}以下是一个完整的PHP函数,用于调用快递100物流查询接口:
function queryExpress($expressNo, $companyCode = '') {
$url = 'https://poll.kuaidi100.com/poll/query.do';
$params = [
'com' => $companyCode,
'num' => $expressNo,
'resultv2' => '1'
];
$postData = json_encode($params);
// 替换为你自己的 key 和 customer
$key = 'your_auth_key';
$customer = 'your_customer_number';
$sign = md5($postData . $key . $customer);
$postDataArr = [
'customer' => $customer,
'param' => $postData,
'sign' => strtoupper($sign)
];
$options = [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postDataArr),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
CURLOPT_TIMEOUT => 30
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if (curl_error($ch)) {
return ['status' => 'error', 'msg' => curl_error($ch)];
}
curl_close($ch);
return json_decode($response, true);
}
// 调用示例
$result = queryExpress('YT123456789', 'yuantong');
print_r($result);
返回结果中常见字段:
实际开发中需要注意以下几点:
以上就是如何实现PHP调用第三方物流API查询订单_PHP第三方物流API(如快递100)订单查询教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号