Logistics even if the query API - PHP articles

Previous article we introduced a logistics service provider, we recommend using a courier birds Interface, describes how to register an account, get a key, can not find the registered address, I made about:

http://kdniao.com/reg

Before also talked how to use the interface provided by the courier birds instant query. Also provides a C # version, Java version, this time we use PHP to implement the interface docking,

Prior to the development, let's look at what is immediate inquiry, as I understand it is that we provide the tracking number and courier company code, and then call the query interface provided by courier birds, you can check the tracking number of shipments, we can learn through this interface to parcel pick-up time, delivery time, the time of receipt, sign, and if the experience to do a little better, can analyze the expected delivery time of this parcel by big data.

Here's what I call real-time query by courier Bird interface provides interfaces available:

 

Appearance display can be ignored, this is a screenshot of my project application, after we get track information, you can press your own page style ultimately presented to customers.

 

Well, now we have to achieve specific chat now!

First, we need to use the resources ready,

Test Merchant ID:

test1617571

 

Testing API key:

554343b2-7252-439b-b4eb-1af42c8f2175 (This Key Test Environment use only)

 

API test address:

http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json

 

 

 

Let's take a look at the interface documentation interpret Express official website provides a bird

 

 

I use this flow chart is a bird in the courier's official website screenshots, let's sort out what several roles

1, the user, this is well understood, is that our group of developers, hard to force the programmer

2, courier birds, the writing is very clear, that is, logistics service providers

3, the customer, this is our own customers, after we get the courier bird data is needed inside the package, through a variety of interfaces to show us the true God to see, once again our customers see the information package, of course, if you information wants to express the birds returned directly exposed to your customers see, I will not object. I can only say that you are the best.

Next, we explain the request parameters

System Level Request Parameters:

 

备注:R-必填(Required),O-可选(Optional),C-报文中该参数在一定条件下可选(Conditional)

这里提到了系统级参数,也相当于是公用参数,这些参数是调用每个接口都是必须要传参的。

接口参数:

 

接口参数,也叫业务参数,请求的业务接口不同,参数的字段,内容也不同,是跟着业务变化的,这里我们实现的是即时查询接口,快递鸟官网要求必须传递快递公司编码和物流单号

物流单号很好理解,就是快递面单上的运单号,快递公司编码是必须传递快递鸟支持的编码,你可能会问,我如何能知道快递鸟支持哪些快递公司,不急,马上告诉你。

下载快递公司编码:

http://www.kdniao.com/documents

上一篇文章有提到,下载下来你就秒懂了,哈哈!

正如我前面的轨迹截图,是一个中通的轨迹数据,中通快递的编码是ZTO,物流单号是78120038107849

业务参数报文组合如下:

{'OrderCode':'','ShipperCode':'ZTO','LogisticCode':'78120038107849'}

 

请求的完整报文:

RequestData=%7b%27OrderCode%27%3a%27%27%2c%27ShipperCode%27%3a%27ZTO%27%2c%27LogisticCode%27%3a%2778120038107849%27%7d&EBusinessID=1617571&RequestType=1002&DataSign=YzBmYTViYmExZmFhOGY1ZTY3MWY5OGFjYWRhNWVjNjU%3d&DataType=2

 

返回的报文信息:

{ "LogisticCode" : "78120038107849", "ShipperCode" : "ZTO", "Traces" : [ { "AcceptStation" : "【济源市】 【济源】(0391-6965909) 的 张霞(18839032214) 已揽收", "AcceptTime" : "2020-01-16 18:30:33" }, { "AcceptStation" : "【济源市】 快件离开 【济源】 已发往 【深圳中心】", "AcceptTime" : "2020-01-16 18:36:41" }, { "AcceptStation" : "【新乡市】 快件已经到达 【新乡中转】", "AcceptTime" : "2020-01-16 22:45:49" }, { "AcceptStation" : "【新乡市】 快件离开 【新乡中转】 已发往 【深圳中心】", "AcceptTime" : "2020-01-16 22:47:48" }, { "AcceptStation" : "【深圳市】 快件已经到达 【深圳中心】", "AcceptTime" : "2020-01-18 04:05:46" }, { "AcceptStation" : "【深圳市】 快件离开 【深圳中心】 已发往 【深圳龙华】", "AcceptTime" : "2020-01-18 08:34:46" }, { "AcceptStation" : "【深圳市】 快件已经到达 【深圳龙华】", "AcceptTime" : "2020-01-18 13:14:10" }, { "AcceptStation" : "【深圳市】 【深圳龙华】 的陈智龙-王颖(13923773902) 正在第1次派件, 请保持电话畅通,并耐心等待(95720为中通快递员外呼专属号码,请放心接听)", "AcceptTime" : "2020-01-18 16:38:35" }, { "AcceptStation" : "【深圳市】 快件已由【丰巢的鑫茂花园A区(丰巢智能快递柜)】代签收, 如有问题请电联(13923773902 / 4000633333,18025858922), 感谢您使用中通快递, 期待再次为您服务!", "AcceptTime" : "2020-01-18 17:32:15" } ], "State" : "3", "EBusinessID" : "1617571", "Success" : true }

 

PHP关键代码:

<?php

//电商ID

defined('EBusinessID') or define('EBusinessID', '请到快递鸟官网申请http://kdniao.com/reg');

//电商加密私钥,快递鸟提供,注意保管,不要泄漏

defined('AppKey') or define('AppKey', '请到快递鸟官网申请http://kdniao.com/reg');

//请求url

defined('ReqURL') or define('ReqURL', 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx');

 

//调用查询物流轨迹

//---------------------------------------------

 

$logisticResult=getOrderTracesByJson();

echo logisticResult;

 

//---------------------------------------------

 

/**

 * Json方式 查询订单物流轨迹

 */

function getOrderTracesByJson(){

       $requestData= "{'OrderCode':'','ShipperCode':'YTO','LogisticCode':'12345678'}";

      

       $datas = array(

        'EBusinessID' => EBusinessID,

        'RequestType' => '1002',

        'RequestData' => urlencode($requestData) ,

        'DataType' => '2',

    );

    $datas['DataSign'] = encrypt($requestData, AppKey);

       $result=sendPost(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)));

}

 

?>

附上详细的接口文档给大家:

http://www.kdniao.com/documents

 

Guess you like

Origin www.cnblogs.com/51api/p/12297427.html