-
首先你要了解curl。
curl是PHP一个很强大的,用来发送http请求的函数 详情请见:https://www.runoob.com/php/php-ref-curl.html。
2.直接进入正题
// n u m b e r 你 查 询 的 快 递 数 p u b l i c f u n c t i o n e x p r e s s ( number 你查询的快递数 public function express( number你查询的快递数publicfunctionexpress(number)
{
if (empty($number)) {
return ajax_error(‘运单号不能为空!’);
}
$host = “http://wuliu.market.alicloudapi.com”; // api访问链接
$path = “/kdi”; // API访问后缀
$method = “GET”;
$appcode =**** //自己的阿里云appcode
h e a d e r s = [ ] ; / / 组 成 请 求 的 h e a r d e r 头 信 息 类 似 于 企 业 签 名 a r r a y p u s h ( headers = []; //组成请求的hearder头信息 类似于企业签名 array_push( headers=[];//组成请求的hearder头信息类似于企业签名arraypush(headers, "Authorization:APPCODE " . $appcode);$querys = "no=" . $number; // 参数写在这里 $url = $host . $path . "?" . $querys; // url拼接 // 初始化curl会话 $curl = curl_init(); // 设置请求方法 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); // 设置请求的url地址 curl_setopt($curl, CURLOPT_URL, $url); // 设置http的头字段 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 将获取的信息以文件流的形式返回(如果不设置此参数,你所得到的数据将会直接输出不能使用) curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//执行
curl_exec($curl) //返回的信息是字符串类型此时你需要json_decode(curl_exec($curl));
转换为json对象来使用
返回的信息参数介绍
object(stdClass)#9 (3) { ["status"] => string(1) "0" //状态吗 ["msg"] => string(2) "ok" ["result"] => object(stdClass)#12 (11) { ["number"] => string(12) "" 快递单号 ["type"] => string(3) "" 快递类型 ["list"] => array(6) { 快递信息 [0] => object(stdClass)#20 (2) { ["time"] => string(19) "" 时间 ["status"] => string(77) "" 快递信息 } [1] => object(stdClass)#21 (2) { ["time"] => string(19) "" ["status"] => string(54) "" } [2] => object(stdClass)#22 (2) { ["time"] => string(19) "" ["status"] => string(80) "" } [3] => object(stdClass)#23 (2) { ["time"] => string(19) "" ["status"] => string(57) "" } [4] => object(stdClass)#24 (2) { ["time"] => string(19) "" ["status"] => string(74) "" } [5] => object(stdClass)#25 (2) { ["time"] => string(19) "" ["status"] => string(112) "" } } } }