Pthon Implementation of Express Bird API Interface for Zhongtong Express Single Query

Keywords: Programming JSON encoding Python network

In the last article, we introduced a logistics service provider. We recommend you to use Express Bird Interface. It mainly describes how to register an account, get a key, and find a registered address. I'll send it:

http://kdniao.com/reg

Today, let's talk about how to use python to do real-time queries using the interface provided by courier birds.

Before we develop, let's first understand what Instant Query is. In my understanding, we provide shipping bill number and courier company code, and then call the query interface provided by courier bird to query the shipping status of shipping bill number. Through this interface, we can know the time when the package was received, sent, received, signed and received, and if the experience is better.One point, large data allows you to analyze the expected delivery time of this package.

Here's what I get by calling the instant query interface provided by the Express Bird interface:

The visual effect is negligible, which is a screenshot of my project application. Once we get track information, we can finally present it to our customers in our own page style.

 

Okay, now let's talk about how it works!

First we have the resources we need.

Test Merchant ID:

test1617571

 

Test API key:

554343b2-7252-439b-b4eb-1af42c8f2175 (This Key is used only in test environments)

 

API test address:

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

Let's first read the interface documentation provided by the Express Bird website

 

Request system-level parameter descriptions:

Remarks: R-Required, O-Optional, C-Message This parameter is optional under certain conditions.

System-level parameters are mentioned here, which are equivalent to common parameters that must be passed in order to call each interface.

Interface parameters:

 

Interface parameters, also known as business parameters, request different business interfaces, parameter fields, content are different, is following the changes of business, here we implement the real-time query interface, courier bird official network requires that the courier company code and logistics number must be passed

Logistics number is well understood, that is, the shipping number on the courier bill of lading. The courier company code is the code that must pass courier bird support. You may ask, how can I know which courier companies courier bird supports? Don't worry, tell you immediately.

Download courier company code:

http://www.kdniao.com/documents

As mentioned in the previous article, download it and you will understand it in seconds, haha!

As in the track screenshot before me, it is a Zhongtong track data, the code of Zhongtong Express is ZTO, and the logistics number is 78120038107849

Business parameter message combinations are as follows:

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

 

Full message requested:

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

 

Message information returned:

 

{
    "LogisticCode": "78120038107849",
    "ShipperCode": "ZTO",
    "Traces": [
        {
            "AcceptStation": "[Jiyuan City [Jiyuan] (0391)-6965909) Zhang Xia of 18839032214 has been collected",
            "AcceptTime": "2020-01-16 18:30:33"
        },
        {
            "AcceptStation": "[Jiyuan City Express departure has been sent to Shenzhen Center",
            "AcceptTime": "2020-01-16 18:36:41"
        },
        {
            "AcceptStation": "[Xinxiang City Express has arrived",
            "AcceptTime": "2020-01-16 22:45:49"
        },
        {
            "AcceptStation": "[Xinxiang City Express departure has been sent to Shenzhen Center",
            "AcceptTime": "2020-01-16 22:47:48"
        },
        {
            "AcceptStation": "[Shenzhen Express has arrived at Shenzhen Center",
            "AcceptTime": "2020-01-18 04:05:46"
        },
        {
            "AcceptStation": "[Shenzhen) Express departure from Shenzhen Center has been sent to Shenzhen Longhua",
            "AcceptTime": "2020-01-18 08:34:46"
        },
        {
            "AcceptStation": "[Shenzhen Express has arrived in Longhua, Shenzhen",
            "AcceptTime": "2020-01-18 13:14:10"
        },
        {
            "AcceptStation": "[Chen Zhilong of Shenzhen Longhua-Wang Ying (13923773902) is dispatching for the first time, Please keep the phone unblocked,And wait patiently (95720 is the exclusive number for outbound calls of Midway couriers, please answer with confidence)",
            "AcceptTime": "2020-01-18 16:38:35"
        },
        {
            "AcceptStation": "[Shenzhen Express has been sent by Xinmao Garden in Fengchao A area(Toyota Smart Express Cabinet)]Sign on behalf of, Please contact us if you have any questions (13923773902) / 4000633333,18025858922), Thank you for using Zhongtong Express, Looking forward to serving you again!",
            "AcceptTime": "2020-01-18 17:32:15"
        }
    ],
    "State": "3",
    "EBusinessID": "1617571",
    "Success": true
}
# Request Data Processing Method
def before_reqData(shipperCode, logisticCode):
    """Request message"""
    frs_reqData = {
        "OrderCode": "",  # Nullable
        "ShipperCode": shipperCode,
        "LogisticCode": logisticCode
    }
    # Convert data to json format
    data = json.dumps(frs_reqData)
    
    # url encoding
    # replace content
    reqData = quote(data).replace("%20%", "%")

    return reqData

def data_sign(shipperCode, logisticCode):
    """autograph datasign"""
    frs_reqData = {
        'OrderCode': '',
        'ShipperCode': shipperCode,
        'LogisticCode': logisticCode
      
    }
  
    APIKey = "554343b2-7252-439b-b4eb-1af42c8f2175";

# Request Content (Unencoded) + APIKey
# Remove spaces before MD5 encryption
    data = json.dumps(frs_reqData).replace(": ", ":").replace(", ", ",") + APIKey

    # md5 encryption
sign_md5 = hashlib.md5(data.encode("utf-8")).hexdigest()

    # base 64 encoding
data_sign = base64.b64encode(sign_md5.encode("utf-8")).decode("utf-8")

    return data_sign

Posted by alant on Wed, 15 Apr 2020 18:02:13 -0700