1. 程式人生 > >電商預約快遞API介面PHP

電商預約快遞API介面PHP

.支援自定義預約時間,不遺漏訂單

.已支援三通一達(申通不支援)、順豐、EMS、宅急送、德邦、優速等物流公司

.快遞公司會在兩小時內聯絡發件人並上門攬件

.PC端,移動端均能對接使用

一、預約快遞API介面

(1)預約快遞API(預約取件介面)是提供給獨立電商、倉儲管理系統、物流供應鏈等物流系統平臺使用的下單介面。 (2)可通過網路選擇快遞公司傳送請求通知快遞公司有快遞要發貨。 (3)訂單編號(OrderCode)不可重複提交,重複提交系統會返回具體錯誤程式碼。 (4)介面支援的訊息接收方式為HTTP POST,請求方法的編碼格式(utf-8):"application/x-www-form-urlencoded;charset=utf-8"。 (5)介面使用:在

快遞鳥網站申請介面KEY

二、示例

請求示例 JSON

{
"OrderCode": "012657018199",
"ShipperCode": "SF",
"PayType": 1,
"MonthCode": "7553045845",
"ExpType": 1,
"Cost": 1.0,
"OtherCost": 1.0,
"Sender": {
  "Company": "LV",
  "Name": "Taylor",
  "Mobile": "15018442396",
  "ProvinceName": "上海",
  "CityName": "上海",
  "ExpAreaName": "青浦區",
  "Address": "明珠路"
},
"Receiver": {
  "Company": "GCCUI",
  "Name": "Yann",
  "Mobile": "15018442396",
  "ProvinceName": "北京",
  "CityName": "北京",
  "ExpAreaName": "朝陽區",
  "Address": "三里屯街道"
},
"Commodity": [
  {
    "GoodsName": "鞋子",
    "Goodsquantity": 1,
    "GoodsWeight": 1.0
  }
],
"AddService": [
  {
    "Name": "COD",
    "Value": "1020"
  }
],
"Weight": 1.0,
"Quantity": 1,
"Volume": 0.0,
"Remark": "小心輕放"
}

返回示例 JSON
失敗:
{
    "EBusinessID": "1237100",
    "Success": false,
    "ResultCode": "105",
    "Reason": "該訂單已經存在,請勿重複操作",
    "UniquerRequestNumber":"451d3c7c-b428-490a-a4ed-e368f15f6c74"
}
成功:
{
    "EBusinessID": "1237100",
    "Success": true,
    "Order": {
        "OrderCode": "012657018199",
        " ShipperCode ": " SF ",
        " LogisticCode ": ""
    },
    "ResultCode": "100",
    "Reason": ""
}

三、demo-PHP

<?php

//電商ID
defined('EBusinessID') or define('EBusinessID', '請申請');
//電商加密私鑰,注意保管,不要洩漏
defined('AppKey') or define('AppKey', '請申請');
//請求url,介面正式地址:http://api.kdniao.cc/api/eorderservice    測試環境地址:http://testapi.kdniao.cc:8081/api/oorderservice
defined('ReqURL') or define('ReqURL', 'http://testapi.kdniao.cc:8081/api/oorderservice');

echo "線上下單";

//構造線上下單提交資訊
$eorder = [];
$eorder["ShipperCode"] = "SF";
$eorder["OrderCode"] = "PM201605078947";
$eorder["PayType"] = 1;
$eorder["ExpType"] = 1;
$sender = [];
$sender["Name"] = "李先生";
$sender["Mobile"] = "18888888888";
$sender["ProvinceName"] = "李先生";
$sender["CityName"] = "深圳市";
$sender["ExpAreaName"] = "福田區";
$sender["Address"] = "賽格廣場5401AB";

$receiver = [];
$receiver["Name"] = "李先生";
$receiver["Mobile"] = "18888888888";
$receiver["ProvinceName"] = "李先生";
$receiver["CityName"] = "深圳市";
$receiver["ExpAreaName"] = "福田區";
$receiver["Address"] = "賽格廣場5401AB";

$commodityOne = [];
$commodityOne["GoodsName"] = "其他";
$commodity = [];
$commodity[] = $commodityOne;

$eorder["Sender"] = $sender;
$eorder["Receiver"] = $receiver;
$eorder["Commodity"] = $commodity;


//呼叫線上下單
$jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
echo "線上下單介面提交內容:<br/>".$jsonParam;
$jsonResult = submitOOrder($jsonParam);

//解析線上下單返回結果
$result = json_decode($jsonResult, true);
echo "<br/><br/>返回碼:".$result["ResultCode"];
if($result["ResultCode"] == "100") {
	echo "<br/>是否成功:".$result["Success"];
}
else {
	echo "<br/>線上下單失敗";
}
//-------------------------------------------------------------


/**
 * Json方式 提交線上下單
 */
function submitOOrder($requestData){
	$datas = array(
        'EBusinessID' => EBusinessID,
        'RequestType' => '1001',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $datas['DataSign'] = encrypt($requestData, AppKey);
	$result=sendPost(ReqURL, $datas);	
	
	//根據公司業務處理返回的資訊......
	
	return $result;
}


 
/**
 *  post提交資料 
 * @param  string $url 請求Url
 * @param  array $datas 提交的資料 
 * @return url響應返回的html
 */
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);
    $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;
}

/**
 * 電商Sign簽名生成
 * @param data 內容   
 * @param appkey Appkey
 * @return DataSign簽名
 */
function encrypt($data, $appkey) {
    return urlencode(base64_encode(md5($data.$appkey)));
}

?>