PHP uses third party to get logistics dynamics instantly

Keywords: JSON PHP

Preface

Recently, there is a requirement for the project to use logistics number to inquire logistics information instantly. From the internet, I know that there are still many third-party APIs about logistics. I choose express bird, which is a free third-party interface. Api integrates more than 400 logistics express interfaces around the world. It is free and unlimited for ever. Express company's multi-channel communication, availability of more than 99.9%, push speed customized configuration, to ensure the stability of access system.

Use process

Log on to the website http://www.kdniao.com/ First of all, we need to register. After registration, in the user management background, there is an application API option. Note that we need real-name authentication before applying for API. After authentication, we can apply for API. Its API interface is very rich.

Because I use real-time query, so the application is the api of real-time query.

Flow chart of api usage

As you can see from the flowchart

  1. Users only need to provide express number and express company
  2. Get the logistics status through api and return the results
  3. We get the results and display them in real time.

API parameters


These are all official API parameters, but we can see the official demo to understand. The official demo is also easy to understand. We can encapsulate it again.

Packaging API

Using API requires three fixed parameters
1. Business id
2. API key
3. Request url,ReqURL

Business id and API key can be seen on my management home page of Express Bird website, and the url request is http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx This can be seen in the interface document.

Main Method

/**
 * @param $ShipperCode Express Company Number
 * @param $order_sn Freight Bill No.
 */
public function getMessage($ShipperCode,$order_sn){
    $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
    );
    //Encryption of $requestData
    $datas['DataSign'] = $this -> encrypt($requestData, self::AppKey);
    $result = $this -> sendPost( self::ReqURL, $datas);
    return $result;
}

In the main method, there are two parameters passed in, one is the express company number, the other is the logistics order number.

We also need to encrypt $requestData, the encrypt method.

/*
 * Encryption
 */
function encrypt($data, $appkey) {
    return urlencode(base64_encode(md5($data.$appkey)));
}

After encryption, it is accessed directly through ReqURL, and the data returned is logistics information.

source code

<?php
/**
 * Use Express Bird api to query
 * User: Administrator
 * Date: 2017/4/22 0022
 * Time: 09:09
 */
class KuaidiController{

    const EBusinessID = 1285564;
    const AppKey = '264ff9e0-2f4c-48d5-877f-1e0670400d18';
    const ReqURL = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";

    /**
     * @param $ShipperCode Express Company Number
     * @param $order_sn Freight Bill No.
     */
    public function getMessage($ShipperCode,$order_sn){
        $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
        );
        //Encryption of $requestData
        $datas['DataSign'] = $this -> encrypt($requestData, self::AppKey);
        $result = $this -> sendPost( self::ReqURL, $datas);
        return $result;
    }

    /**
     *  post Submission of data
     * @param  string $url Request Url
     * @param  array $datas Data submitted
     * @return url html returned from the response
     */
    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;
    }


    /*
     * Encryption
     */
    function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }
}
$model = new KuaidiController();
$res = $model -> getMessage('ZTO','12345678');
echo "<pre>";
var_dump($res);

Posted by vaska on Fri, 05 Jul 2019 14:48:58 -0700