MENU

记一次中通快递接口解析-PHP

• July 29, 2019 • Read: 596 • 经验分享,原创作品阅读设置

也不知道发啥文章,今天就再水一下吧
流量卡发货大多数都走的中通快递,我去官网看了一下抓到了接口:

https://hdgateway.zto.com/WayBill_GetDetail?billCode=订单号

解析以后的数据大概就是这样了:
申通快递接口解析
里面有些无用信息,然后快递信息是个二维数组,我们就直接开始解析吧!
直接使用foreach去遍历快递信息数据:

foreach ($arr as $k=>$v){
    foreach ($v as $value){
        $arrl[]=array(
            "time"=>$value->scanDate,
            "signMan"=>$value->signMan,
            "operateUserPhone"=>$value->operateUserPhone,
            "date"=>$value->date,
            "operateUser"=>$value->operateUser,
            "scanType"=>$value->scanType,
            "stateDescription"=>$value->stateDescription
        );
    }
}

因为里面是两个数组使用用了foreach嵌套foreach去遍历到两个数组里面的数据,把循环的数据存储在$arrl[]里只保留有用的信息,很多信息都是没用的,拿到快递信息之后对数组进行重建看起来完整一点,不然就一个快递信息看起来不完整:

$output = [
    "code"=>1,
    "msg"=>"查询成功!",
    "logisticsRecord"=>$arrl,
    "billPrescription"=>$json->result->billPrescription,
    "billCode"=>$json->result->billCode,
    "author"=>"教书先生"
];

完整示例代码:

    <?php
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');
error_reporting(0);
function myCurl($url, $ip){
    $ch = curl_init();     // Curl 初始化
    $ua='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36';    // 伪造抓取 UA
    curl_setopt($ch, CURLOPT_URL, $url);              // 设置 Curl 目标
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      // Curl 请求有返回的值
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);     // 设置抓取超时时间
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);        // 跟踪重定向
    curl_setopt($ch, CURLOPT_ENCODING, "");    // 设置编码
    curl_setopt($ch, CURLOPT_REFERER, $url);   // 伪造来源网址
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //伪造IP
    curl_setopt($ch, CURLOPT_USERAGENT, $ua);   // 伪造ua
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // 取消gzip压缩
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $content = curl_exec($ch);
    curl_close($ch);    // 结束 Curl
    return $content;    // 函数返回内容
}
function GetClientIP(){
    $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    if($ip != ""){
        $arr = explode(",",$ip);
        return $arr[0];
    }else{
        return $_SERVER["REMOTE_ADDR"];
    }
}
!empty($_GET['num'])  ? $_GET['num'] :exit(json_encode([
    "code"=>-1,
    "msg"=>"请输入订单号!"
],JSON_UNESCAPED_UNICODE));
$json = json_decode(myCurl("https://hdgateway.zto.com/WayBill_GetDetail?billCode=".$_GET['num'],GetClientIP()));
if ($json->message!=="快递信息查询成功" || $json->result==null){
    $output = [
        "code"=>-1,
        "msg"=>"查询失败"
    ];
} else {
    $arr = $json->result->logisticsRecord;
    foreach ($arr as $k=>$v){
        foreach ($v as $value){
            $arrl[]=array(
                "time"=>$value->scanDate,
                "signMan"=>$value->signMan,
                "operateUserPhone"=>$value->operateUserPhone,
                "date"=>$value->date,
                "operateUser"=>$value->operateUser,
                "scanType"=>$value->scanType,
                "stateDescription"=>$value->stateDescription
            );
        }
    }
    $output = [
        "code"=>1,
        "msg"=>"查询成功!",
        "logisticsRecord"=>$arrl,
        "billPrescription"=>$json->result->billPrescription,
        "billCode"=>$json->result->billCode,
        "author"=>"教书先生"
    ];
}
exit(json_encode($output,JSON_UNESCAPED_UNICODE));

请求方式:GET 参数:num num为订单号

index.php?num=73113244809048
Last Modified: August 6, 2019
Archives QR Code Tip
QR Code for this page
Tipping QR Code