Express bird platform, query express information. Escaped pit

Keywords: PHP MySQL JSON Apache

Express bird platform, query express information. Escaped pit

Written in front

Technology is updated and iterated, but there are always traces to follow.
Endless wheel building has been exhausting.
The pit that oneself jumps over, share final result, just for letting pain no longer repeat.

One background

In a mall project, you need to query the logistics information according to the order number. After their own express bill number evaluation, the final selection of express bird platform.

Two steps

1. You need to apply for an account at the express bird platform. Gets the background ID and the encrypted private key.
2. Integrate your own class library according to the official demo.
3 call in the background and render the result in the foreground.

3. References

Express bird development document Express bird development document

IV. development environment

apache and mysql and php5.6
Framework: thinkp5.0

5. Code and notes

1 encapsulated class library

E-commerce ID and encrypted private key can be made into configuration file

<?php

//Express bird platform, query express information according to express number and express company code
class BirdExpress
{
    private $EBusinessID;       //Electricity supplier ID
    private $AppKey;            //E-commerce encrypts private key, which is provided by express bird. Please take care of it and don't disclose it
    private $ReqURL;            //Request ur1 to fill in your account data

    public function __construct()
    {

        //Replace configuration information
        $this->EBusinessID = "";
        $this->AppKey = "";
        $this->ReqURL = "http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
    }

    /**
     * Core method
     * Json Query order logistics track by
     * @param string $ShipperCode Express company code
     * @param string $LogisticCode Logistics number
     * @param int $arrayFlag Array conversion flag default 0: No 1: conversion
     * Examples of reference rules:
     * $requestData = "{'OrderCode':'','ShipperCode':'ZTO','LogisticCode':'640041334612'}";
     * @return result Json data with real-time logistics information
     * * status
     * 0:There is no result in the logistics bill No;
     * 3:On the way, express delivery is in the process of transportation;
     * 4:Take delivery, express has been taken over by express companies and generated the first piece of information;
     * 5:Problems in the process of express mailing;
     * 6:Signed by the addressee;
     * 7:Return, express delivery due to user refused to sign, beyond the zone and other reasons, and the sender has signed;
     * 8:Dispatch, the courier is in the city dispatch;
     * 9:Return, the goods are on the way back to the sender;
     */
    public function getOrderTracesByJson($ShipperCode = "",$LogisticCode = "",$arrayFlag = 0)
    {
        $requestData = "{   
                            'OrderCode':'',
                            'ShipperCode':'$ShipperCode',
                            'LogisticCode':'$LogisticCode'
                        }";
        $postData = array(
            'EBusinessID' => $this->EBusinessID,
            'RequestType' => '1002',//Interface instruction 1002
            'RequestData' => urlencode($requestData),
            'DataType' => '2',
        );
        $postData['DataSign'] = $this->encrypt($requestData, $this->AppKey);
        $result = $this->sendPost($this->ReqURL, $postData);
        //Information returned according to the company's business processing
        if ($arrayFlag){
            $result = json_decode($result,true);
            //TODO facilitates reverse display of logistics information and array inversion
            $result['Traces'] = array_reverse($result['Traces']);
        }
        return $result;
    }

    /**
     * E-commerce Sign generation
     * @param data content
     * @param appkey Appkey
     * @return DataSign autograph
     */
    private function encrypt($data, $appkey) {
        return urlencode(base64_encode(md5($data.$appkey)));
    }

    /**
     *  post Submit data
     * @param  string $url Request Url
     * @param  array $datas Submitted data
     * @return url html returned in response
     */
    private 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;
    }




}


2 controller call

Express bird has the excel table corresponding to the code of express company, which is imported into mysql table through php tool class

public function getBirdExpress()
    {
        //According to the search table of express company, express code
        $kuaidiName = 'Express delivery';
        $ShipperCode = Db::name('xbird_express')->where('name',$kuaidiName)->value('code');
        //Odd Numbers
        $LogisticCode = "YT4348678055964";
        $express = new BirdExpress();
        $expressData = $express->getOrderTracesByJson($ShipperCode,$LogisticCode,1);
        halt($expressData);

    }

3 return data

Sensitive information involved in the returned data, modified
You can render the foreground template according to the returned data

array(6) {
  ["LogisticCode"] => string(15) "*********"
  ["ShipperCode"] => string(3) "YTO"
  ["Traces"] => array(11) {
    [0] => array(2) {
      ["AcceptStation"] => string(156) "Signed by customer: *** Thank you for using Yuantong express. We are looking forward to serving you again. If you have any questions, please contact: 1523***835´╝îComplaints Hotline:*******"
      ["AcceptTime"] => string(19) "2020-01-16 17:09:02"
    }
    [1] => array(2) {
      ["AcceptStation"] => string(98) "****Yuantong agency***The post station has sent a self delivery message. Please call 185****490"
      ["AcceptTime"] => string(19) "2020-01-16 09:00:51"
    }
    [2] => array(2) {
      ["AcceptStation"] => string(77) "Express has arrived****Yuantong agency***Post station contact number 185****0490"
      ["AcceptTime"] => string(19) "2020-01-16 07:52:57"
    }
    [3] => array(2) {
      ["AcceptStation"] => string(49) "[Zhengzhou City, Henan Province******Company] received"
      ["AcceptTime"] => string(19) "2020-01-16 07:45:53"
    }
    [4] => array(2) {
      ["AcceptStation"] => string(84) "[Zhengzhou transfer center has sent out the next station [Zhengzhou City, Henan Province]*****Company]"
      ["AcceptTime"] => string(19) "2020-01-15 22:18:26"
    }
    [5] => array(2) {
      ["AcceptStation"] => string(40) "[Zhengzhou transfer center company] income"
      ["AcceptTime"] => string(19) "2020-01-15 21:57:06"
    }
    [6] => array(2) {
      ["AcceptStation"] => string(75) "[Yiwu transfer center has sent out the next station [Zhengzhou transfer center company]"
      ["AcceptTime"] => string(19) "2020-01-15 00:51:08"
    }
    [7] => array(2) {
      ["AcceptStation"] => string(40) "[Yiwu transfer center company] income"
      ["AcceptTime"] => string(19) "2020-01-15 00:25:12"
    }
    [8] => array(2) {
      ["AcceptStation"] => string(90) "[Honghua, Yiwu City, Jinhua City, Zhejiang Province has sent out the next station [Yiwu transfer center company]"
      ["AcceptTime"] => string(19) "2020-01-15 00:15:18"
    }
    [9] => array(2) {
      ["AcceptStation"] => string(55) "[Yiwu City, Jinhua City, Zhejiang Province****Company] packed"
      ["AcceptTime"] => string(19) "2020-01-15 00:12:46"
    }
    [10] => array(2) {
      ["AcceptStation"] => string(90) "[Yiwu City, Jinhua City, Zhejiang Province***Company] received by: **** (185*****134)"
      ["AcceptTime"] => string(19) "2020-01-15 00:07:28"
    }
  }
  ["State"] => string(1) "3"
  ["EBusinessID"] => string(7) "*******"
  ["Success"] => bool(true)
}

Six at the end

I applied for the experience account of express bird.
Limited to 500 times / day (only 3 Shentong, Yuantong and Zhongtong are supported for instant query)
Unlimited dosage (electronic sheet)
If your business needs, you can upgrade.

Published 1 original article, praised 0 and visited 3
Private letter follow

Posted by ashrust on Mon, 17 Feb 2020 20:45:30 -0800