PHP 之快遞100接口封裝

<?php
/**
 * Created by PhpStorm.
 * User: Yang
 * Date: 2019/8/23
 * Time: 10:38
 */

class Kuaidi_Query
{
    private $_query_url = 'http://poll.kuaidi100.com/poll/query.do';    //實時查詢請求地址
//    private $_url = 'http://www.kuaidi100.com/autonumber/auto?num=906919164534&key=IobfFnLz2751';    //實時查詢請求地址
    private $_auto_url = 'http://www.kuaidi100.com/autonumber/auto';    //實時查詢請求地址
    private $_key = "";
    private $_customer = "";
    private $_params = array();

    public function __construct($num, $com = '', $phone = '', $from = '', $to = '', $resultv2 = 1)
    {
        if (empty($com)) {
            //歸屬公司智能判斷
            $com = $this->check($num);
        }
        $params = array(
            'com' => $com,                    //快遞公司編碼
            'num' => $num,                    //快遞單號
            'phone' => $phone,                //手機號
            'from' => $from,                //出發地城市
            'to' => $to,                    //目的地城市
            'resultv2' => $resultv2        //開啓行政區域解析
        );
        $this->_params = $params;
    }

    /**
     * 單號歸屬公司智能判斷接口
     * @param $num
     * @return mixed
     */
    public function check($num)
    {
        $url = $this->_auto_url."?num=".$num."&key=".$this->_key;
        $data = $this->get_curl($url);
        $data_array = json_decode($data, true);
        return $data_array[0]['comCode'];
    }

    /**
     * 執行快遞查詢接口
     * @return mixed
     */
    public function Query()
    {
        $post_data = array();
        $post_data["customer"] = $this->_customer;
        $post_data["param"] = json_encode($this->_params);
        $sign = md5($post_data["param"] . $this->_key . $post_data["customer"]);
        $post_data["sign"] = strtoupper($sign);
        $params = "";
        foreach ($post_data as $k => $v) {
            $params .= "$k=" . urlencode($v) . "&";        //默認UTF-8編碼格式
        }
        $post_data = substr($params, 0, -1);
        $query_data = $this->post_curl($this->_query_url, $post_data);
        return $query_data;
    }

    /**
     * get 請求
     * @param $url
     * @return mixed
     */
    public function get_curl($url) {
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//訪問的URL
        curl_setopt($ch, CURLOPT_HEADER, false);//設置不須要頭信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只獲取頁面內容,但不輸出
        $result = curl_exec($ch);//執行請求
        curl_close($ch);//關閉curl,釋放資源
        $data = str_replace("\"", '"', $result);
        return $result;
    }

    /**
     * post 請求
     * @param $post_data
     * @return mixed
     */
    public function post_curl($url, $post_data)
    {
        //發送post請求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        $data = str_replace("\"", '"', $result);
        $data = json_decode($data, true);
        return $data;
    }
}

 使用方式:php

<?php
/**
 * Created by PhpStorm.
 * User: Yang
 * Date: 2019/8/23
 * Time: 11:03
 */

include "../../Kuaidi_Query.php";

$k = new Kuaidi_Query(3950055201640);
$data = $k->Query();
if ($data['message'] == "ok") {
    $list = $data['data'];
    var_dump($list);
}else{
    var_dump($data['message']);
}

結果展現:json

array(11) {
  [0]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-23 13:21:54"
    ["context"]=>
    string(115) "【武漢市】 已簽收 : 由董雨昂(金龍·四季陽光 )代簽收,若有問題聯繫董雨昂(13147108459)"
    ["ftime"]=>
    string(19) "2019-03-23 13:21:54"
    ["areaCode"]=>
    NULL
    ["areaName"]=>
    NULL
    ["status"]=>
    string(6) "簽收"
  }
  [1]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-23 09:13:51"
    ["context"]=>
    string(106) "【武漢市】 湖北武漢黃陂區公司楊園分部 派件員 董雨昂(13147108459)正在爲您派送"
    ["ftime"]=>
    string(19) "2019-03-23 09:13:51"
    ["areaCode"]=>
    string(14) "CN420116000000"
    ["areaName"]=>
    string(26) "湖北,武漢市,黃陂區"
    ["status"]=>
    string(6) "派件"
  }
  [2]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-23 06:55:27"
    ["context"]=>
    string(113) "【武漢市】 已離開 湖北武漢黃陂區公司 發往 湖北武漢黃陂區公司楊園分部(15391528386)"
    ["ftime"]=>
    string(19) "2019-03-23 06:55:27"
    ["areaCode"]=>
    NULL
    ["areaName"]=>
    NULL
    ["status"]=>
    string(6) "在途"
  }
  [3]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-23 06:52:47"
    ["context"]=>
    string(72) "【武漢市】 已到達 湖北武漢黃陂區公司 立刻爲您派送"
    ["ftime"]=>
    string(19) "2019-03-23 06:52:47"
    ["areaCode"]=>
    string(14) "CN420116000000"
    ["areaName"]=>
    string(26) "湖北,武漢市,黃陂區"
    ["status"]=>
    string(6) "派件"
  }
  [4]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-22 20:30:30"
    ["context"]=>
    string(50) "【武漢市】 已到達 湖北武漢分撥中心"
    ["ftime"]=>
    string(19) "2019-03-22 20:30:30"
    ["areaCode"]=>
    string(14) "CN420100000000"
    ["areaName"]=>
    string(16) "湖北,武漢市"
    ["status"]=>
    string(6) "在途"
  }
  [5]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-22 20:30:27"
    ["context"]=>
    string(85) "【武漢市】 已離開 湖北武漢分撥中心 發往 湖北武漢黃陂區公司"
    ["ftime"]=>
    string(19) "2019-03-22 20:30:27"
    ["areaCode"]=>
    NULL
    ["areaName"]=>
    NULL
    ["status"]=>
    string(6) "在途"
  }
  [6]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-22 20:15:30"
    ["context"]=>
    string(50) "【武漢市】 已到達 湖北武漢分撥中心"
    ["ftime"]=>
    string(19) "2019-03-22 20:15:30"
    ["areaCode"]=>
    string(14) "CN420100000000"
    ["areaName"]=>
    string(16) "湖北,武漢市"
    ["status"]=>
    string(6) "在途"
  }
  [7]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-22 00:48:37"
    ["context"]=>
    string(82) "【汕頭市】 已離開 廣東揭陽分撥中心 發往 湖北武漢分撥中心"
    ["ftime"]=>
    string(19) "2019-03-22 00:48:37"
    ["areaCode"]=>
    NULL
    ["areaName"]=>
    NULL
    ["status"]=>
    string(6) "在途"
  }
  [8]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-22 00:46:46"
    ["context"]=>
    string(50) "【汕頭市】 已到達 廣東揭陽分撥中心"
    ["ftime"]=>
    string(19) "2019-03-22 00:46:46"
    ["areaCode"]=>
    string(14) "CN440500000000"
    ["areaName"]=>
    string(16) "廣東,汕頭市"
    ["status"]=>
    string(6) "在途"
  }
  [9]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-21 20:32:50"
    ["context"]=>
    string(94) "【汕頭市】 已離開 廣東汕頭潮陽區公司陳店分部 發往 湖北武漢網點包"
    ["ftime"]=>
    string(19) "2019-03-21 20:32:50"
    ["areaCode"]=>
    NULL
    ["areaName"]=>
    NULL
    ["status"]=>
    string(6) "在途"
  }
  [10]=>
  array(6) {
    ["time"]=>
    string(19) "2019-03-21 19:05:50"
    ["context"]=>
    string(65) "【汕頭市】 廣東汕頭潮陽區公司陳店分部 已攬收"
    ["ftime"]=>
    string(19) "2019-03-21 19:05:50"
    ["areaCode"]=>
    string(14) "CN440513000000"
    ["areaName"]=>
    string(26) "廣東,汕頭市,潮陽區"
    ["status"]=>
    string(6) "攬收"
  }
}
相關文章
相關標籤/搜索