PHP uses third-party instant access to logistics dynamics

Source: Internet
Author: User
Tags getmessage urlencode

Objective

Recently done the project has a request, to use the logistics number to immediately query the logistics information, from the Internet, about the third party API logistics is still a lot of, and I choose to express Bird, express Bird is a free third-party interface, API integration of more than 400 global Logistics Express interface, permanent free unlimited time, Interface and courier company Multi-channel communication, the availability of more than 99.9%, push speed customized configuration to ensure the stability of the access system.

Use procedure

Login website http://www.kdniao.com/First to register, after registration, in the user management background, there is a request API options, Note that the application API needs real -name authentication, after certification can apply for API, Its API interface is still very rich.

Because if I use an instant query, the application is an instant query API.

Flowchart of API Usage

From the flowchart, you can see

    1. Users only provide courier number and courier company
    2. Get the logistics status through the API and return the results
    3. We get the results and perform real-time processing of the display.
API parameters


These are the official API parameters, but we can read the official demo. The official demo is also easy to understand. We can wrap it up again.

Encapsulation API

Three fixed parameters required for use of API
1. Merchant ID
2. API Key
3. Request Url,requrl

Merchant ID and API key can be found on the My admin homepage of the Express Bird website, and the request URL is http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx, which can be seen in the interface document.

Main method

/** * @param $ShipperCode 快递公司编号 * @param $order_sn 运单号 */public function getMessage($ShipperCode,$order_sn){    $requestData= "{‘OrderCode‘:‘‘,‘ShipperCode‘:‘".$ShipperCode."‘,‘LogisticCode‘:‘".$order_sn."‘}";    $datas = array(        ‘EBusinessID‘ => self::EBusinessID,        ‘RequestType‘ => ‘1002‘,//接口指令1002,固定        ‘RequestData‘ => urlencode($requestData) ,        ‘DataType‘ => ‘2‘, //数据返回格式 2 json    );    //把$requestData进行加密处理    $datas[‘DataSign‘] = $this -> encrypt($requestData, self::AppKey);    $result = $this -> sendPost( self::ReqURL, $datas);    return $result;}

In this main method, there are two parameters passed in, one is The courier company number, and the other is the logistics order number.

We also need to encrypt the $requestdata, which is the Encrypt method.

/* * 进行加密 */function encrypt($data, $appkey) {    return urlencode(base64_encode(md5($data.$appkey)));}

After the encryption is accessed directly through the Requrl, the returned data is the logistics information.

Source
<?php/** * Query using Express Bird API * User:administrator * DATE:2017/4/22 0022 * time:09:09 */class kuaidicontroller{const E    Businessid = 1285564;    Const AppKey = ' 264ff9e0-2f4c-48d5-877f-1e0670400d18 ';    Const REQURL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx"; /** * @param $ShipperCode Courier Company Number * @param $order _sn waybill Number * * Public function GetMessage ($ShipperCode, $order _s        N) {$requestData = "{' Ordercode ': ', ' shippercode ': '". $ShipperCode. "', ' logisticcode ': '". $order _sn. "'}";            $datas = Array (' ebusinessid ' = = self::ebusinessid, ' requesttype ' = ' 1002 ',//interface instruction 1002, fixed        ' RequestData ' = UrlEncode ($requestData), ' DataType ' = ' 2 ',//Data Return format 2 JSON);        Encrypt the $requestdata $datas [' datasign '] = $this, encrypt ($requestData, self::appkey);        $result = Sendpost (Self::requrl, $datas), $this;    return $result; }/** * Post submission Data * @param string $urlRequest URL * @param array $datas submitted data * @return URL response returns the HTML */function Sendpost ($url, $datas) {$temp        s = 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; }/* * Encrypt */function Encrypt ($data, $appkey) {return UrlEncode (Base64_encode (MD5 ($data. $appkey)))    ; }} $model = new Kuaidicontroller (); $res = $model, GetMessage (' Zto ', ' 12345678 '); echo "<pre>"; Var_dump ($res);

PHP uses third-party instant access to logistical updates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.