PHP 对接快递100接口(基础接口查询封装)

记录一下PHP 对接快递100接口(基础接口查询)

<?php
namespace app\common\web;
class Courier
{
    private $_query_url = 'http://poll.kuaidi100.com/poll/query.do';    //实时查询请求地址
    private $_auto_url = 'http://www.kuaidi100.com/autonumber/auto';    //归属公司智能判断
    private $_key = "";//客户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);
        if ($data_array['returnCode']==200){
            return $data_array[0]['comCode'];
        }else{
            return $data_array['message'];
        }
    }

    /**
     * 执行快递查询接口
     * @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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是誰萆微了承諾

你的鼓励是对我最大的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值