51tracking Webhooks

Webhook 推送 

Webhook URL
Learn more about   format of inbound webhooks
Version
Callback
If selecting "true", then there must return a "200" response!

什么是 Webhook?

     Webhook就是用户通过自定义回调函数的方式来改变Web应用的一种行为。通过Webhook,您可以自定义一些行为通知到指定的URL去。当请求数据完成,Webhook会通过您设置的URL将对应数据响应给您。

 Webhook V2

     当查询状态有更新,且触发了你在设置中的状态更新提醒时,系统会做出一个POST请求发送到你在webhook页面自定义的回调url上。其中,POST主体包含了以下数据的JSON字符串。

     目前,我们支持HTTP和HTTPS链接来确保启用SSL链接时网站的安全性。但是请记住,此时你的终端在互联网上将是完全开放的。

     签名验证:

     为了确保消息的来源身份是 51tracking, 你可以选择对 POST 数据的来源进行安全认证. ( 不验证, 直接解析 POST 的数据也可以 ).安全认证的方法如下:

     1. 解析出 POST 数据中的 timeStrsignature 这两个参数。

     2. 使用你的51tracking用户邮箱(密钥)timeStr(明文) 生成签名 signature, 与 POST 数据中的 signature 进行校验 ( 签名算法: PHP:SHA256、JAVA:HmacSHA256)

PHP 示例代码:
function verify($timeStr,$useremail,$signature){
    $hash="sha256";
    $result=hash_hmac($hash,$timeStr,$useremail);
    return strcmp($result,$signature)==0?1:0;
}

     How to use:

       Step 1:你应该先在你的用户后台设置你的webhook url 和需要提醒的状态! my.51tracking.com/webhook_setting.php

       Step 2:当我们服务器检测到你的单号更新且满足你的设置的时候,我们就会将更新后的数据推送到你设置的webhook url路径上。所以你需要预先在这个webhook url写好你自己的程序,对接收到的webhook数据进行任意操作。当我们推送过来的时候你的程序就会执行一次。

       Step 3:如何获取webhook推送过来的数据,在php中只需要$inputJSON = file_get_contents("php://input"); $inputJSON就是我们推送过来的数据。如下是我们获取数据并写入文件的例子,当然你也可以将它存入数据库。


  •   Example Code  
  • Write response data into file
    
    $handle    = fopen('test.log','a+'); 
    $inputJSON = file_get_contents("php://input");
    if(!empty($inputJSON)){
    	fwrite($handle, date("Y-m-d H:i:s").":  ".$inputJSON . "\r\n");
    	echo 200;
    }else{
    	fwrite($handle, date("Y-m-d H:i:s").": can not get webhook data!\r\n");
    }
    
  • Response  200
  • Headers
    Content-Type: application/json
    Body
    {
    "meta":{
       "code":200,
       "type":"Success",
       "message":"Success"
    },
    "data":{
    	"id":"7eabcb811fa10ab1f77d10418579a7f5",
    	"tracking_number":"LX203960974CN",
    	"carrier_code":"china-ems",
    	"status":"exception",
    	"created_at":"2016-11-09T22:21:43+08:00",
    	"updated_at":"2016-11-16T00:13:34+08:00",
    	"title":"","order_id":null,
    	"customer_name":null,
    	"customer_email":"",
    	"original_country":"China",
    	"destination_country":"Sweden",
    	"itemTimeLength":18,
    	"origin_info":{
    		"weblink":"http:\/\/www.ems.com.cn\/",
    		"phone":null,
    		"carrier_code":"china-ems",
    		"trackinfo":[
    		{
    			"Date":"2016-11-15 08:23",
    			"StatusDescription":"\u672a\u59a5\u6295",
    			"Details":"\u745e\u5178"
    		},{
    			"Date":"2016-11-14 09:34",
    			"StatusDescription":"\u5230\u8fbe\u5904\u7406\u4e2d\u5fc3,\u6765\u81ea\u4e2d\u56fd \u5e7f\u5dde",
    			"Details":"\u745e\u5178"
    		},{
    			"Date":"2016-10-31 22:45",
    			"StatusDescription":"\u6d77\u5173\u653e\u884c",
    			"Details":"\u5e7f\u5dde\u5e02"
    		},{
    			"Date":"2016-10-31 22:35",
    			"StatusDescription":"\u9001\u4ea4\u6d77\u5173",
    			"Details":"\u5e7f\u5dde\u5e02"
    		},{
    			"Date":"2016-10-29 22:04",
    			"StatusDescription":"\u5230\u8fbe\u5e7f\u5dde\u822a\u7ad9\u5904\u7406\u4e2d\u5fc3\uff08\u7ecf\u8f6c\uff09",
    			"Details":"\u5e7f\u5dde\u5e02"
    		},{
    			"Date":"2016-10-29 20:20",
    			"StatusDescription":"\u79bb\u5f00\u4e2d\u5c71\u5e02 \u53d1\u5f80\u5e7f\u5dde\u5e02",
    			"Details":"\u4e2d\u5c71\u5e02"
    		},{
    			"Date":"2016-10-29 16:24",
    			"StatusDescription":"\u4e2d\u5c71\u5e02\u90ae\u653f\u901f\u9012\u7269\u6d41\u516c\u53f8\u6052\u57fa\u901f\u9012\u90e8\u5df2\u6536\u4ef6\uff08\u63fd\u6295\u5458\u59d3\u540d\uff1a\u9ad8\u4f1f\u5065,\u8054\u7cfb\u7535\u8bdd:13631122287\uff09",
    			"Details":"\u4e2d\u5c71\u5e02"
    		}]
    	},
    	"lastEvent":"\u672a\u59a5\u6295,\u745e\u5178,2016-11-15 08:23"
    },
    "verifyInfo":{
      "timeStr":1488249109,
      "signature":"4b279021f5c041f6e3344e7a0636cc26201ab24b91adcea6d38331cb89221d45"
    }
    }
    

查物流轨迹,就用51Tracking