目录
一、快宝第三方
第三方快递api还蛮多的,选择快宝的原因是因为老板觉得价格便宜......
物流单号查询|物流跟踪API|运单号查询|运单查询API文档_快宝开放平台
二、使用步骤
1.物流查询
/*
@method 物流查询接口
@param string waybill_codes 物流单号
*/
function express_query($waybill_code)
{
// 公共参数设置
// 正式地址
$host = "https://kop.kuaidihelp.com/api";
$headers = array();
// Content-Type定义
array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");
$appId = "appId"; // 替换成你真实的值
$ts = time();
$appKey = "appKey";// 替换成你真实的值
// 接口名称
$method = "express.info.get";
// 请求参数设置
$data = [
"waybill_codes" => $waybill_code,
"result_sort" => 0,
];
// 参数主体
$bodys = [
"app_id" => $appId,
"method" => $method,
"sign" => md5($appId . $method . $ts . $appKey),
"ts" => $ts,
"data" => json_encode($data)
];
$bodys = http_build_query($bodys);
$url = $host;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$" . $host, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
echo (curl_exec($curl));
}
2.智能识别单号品牌
/*
@method 物流查询接口
@param string waybill_codes 物流单号
*/
function express_query($waybill_code)
{
// 公共参数设置
// 正式地址
$host = "https://kop.kuaidihelp.com/api";
$headers = array();
// Content-Type定义
array_push($headers, "Content-Type" . ":" . "application/x-www-form-urlencoded; charset=UTF-8");
$appId = "appId";
$ts = time();
$appKey = "appKey";
// 接口名称
$method = "express.brand.identify";
// 智能识别单号品牌
$data = [
"waybill_code" => $waybill_code
];
// 参数主体
$bodys = [
"app_id" => $appId,
"method" => $method,
"sign" => md5($appId . $method . $ts . $appKey),
"ts" => $ts,
"data" => json_encode($data)
];
$bodys = http_build_query($bodys);
$url = $host;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$" . $host, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
echo (curl_exec($curl));
}
3.合并
物流查询
识别单号品牌
除了以上截图的地方有区别其他是相同的代码,可以多接收一个type或则别的用来区分参数,通过if判断一下请求哪一个方法,将他们合并成一个方法
// 订单参数设置
if ($type == 1) {
// 接口名称
$method = "express.info.get";
// 物流查询
$data = [
"waybill_codes" => $waybill_code,
"result_sort" => 0,
];
} else if ($type == 2) {
// 接口名称
$method = "express.brand.identify";
// 智能识别单号品牌
$data = [
"waybill_code" => $waybill_code
];
}