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
/**
 * 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']);
}

结果展示:

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) "揽收"
  }
}

 

posted @ 2019-08-23 13:09  样子2018  阅读(1092)  评论(0编辑  收藏  举报