Thinkphp3.2 查询物流接口对接(快递鸟为例)

接入流程

1、注册账号 http://www.kdniao.com/reg 取得用户IDAPI key

2、申请服务,

3、接口对接

后台代码

具体使用的时候,调用接口只需要改几个参数——封装的三个参数(在注册认证完以后会自动生成),ShipperCode:快递公司编码,LogisticCode:物流运单号

如果是针对很多家物流公司的,在调用此接口之前,判断快递公司,从而传输对应的快递公司编码及物流单号

Thinkphp3.2 查询物流接口对接(快递鸟为例)_第1张图片

写一个公共函数

KdApiSearch.php
 $this->EBusinessID,
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $datas['DataSign'] = $this->encrypt($requestData, $this->AppKey);
        $result=$this->sendPost($this->ReqURL, $datas);
        //根据公司业务处理返回的信息......
        return $result;
    }

    /**
     *  post提交数据
     * @param  string $url 请求Url
     * @param  array $datas 提交的数据
     * @return url响应返回的html
     */
    function sendPost($url, $datas) {
        $temps = array();
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);
        }
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
        if(empty($url_info['port']))
        {
            $url_info['port']=80;
        }
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
        $headerFlag = true;
        while (!feof($fd)) {
            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
                break;
            }
        }
        while (!feof($fd)) {
            $gets.= fread($fd, 128);
        }
        fclose($fd);

        return $gets;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }



}

调用KdApiSearch.php(以TP3.2为例)

/** 查看物流
     * @param $courier_no 运单号
     * @param $type 快递公司编码
     */
    public function see_logistics($courier_no,$type){
        require './php-sdk/KdApiSearch.php';
        $dd = new \KdApiSearch();
        $isOrder = $dd->getOrderTracesByJson($courier_no,$type);
        $kdres = json_decode($isOrder,true);
        foreach ($isOrder as $key=>$res){
            $kdres[$key] = json_decode($res,true);
        }
        return $kdres;
    }

项目结构

Thinkphp3.2 查询物流接口对接(快递鸟为例)_第2张图片

你可能感兴趣的:(php,API,微信小程序)