基于PHP的圆通电子面单接口对接
基于PHP的圆通电子面单接口对接
1)使用:适用于多个快递电子面单打印打印需求
2)接口类型:快递鸟电子面单接口
3)接口秘钥地址:https://www.360docs.net/doc/fe17544651.html,/reg
4)接口说明:接口支持的消息接收方式为HTTP POST
5)请求方法的编码格式(utf-8):
"application/x-www-form-urlencoded;charset=utf-8"。
PHP案例
//电商ID
defined('EBusinessID') or define('EBusinessID', '申请
https://www.360docs.net/doc/fe17544651.html,/ServiceApply.aspx');
//电商加密私钥,注意保管,不要泄漏
defined('AppKey') or define('AppKey', '申请
https://www.360docs.net/doc/fe17544651.html,/ServiceApply.aspx');
//请求url,接口正式地址:https://www.360docs.net/doc/fe17544651.html,/api/Eorderservice defined('ReqURL') or define('ReqURL',
'https://www.360docs.net/doc/fe17544651.html,:8081/api/Eorderservice');
//调用获取物流轨迹
//-------------------------------------------------------------
//构造电子面单提交信息
$eorder = [];
$eorder["ShipperCode"] = "YTO";
$eorder["OrderCode"] = "PM201604062341";
$eorder["PayType"] = 1;
$eorder["ExpType"] = 1;
$sender = [];
$sender["Name"] = "李先生";
$sender["Mobile"] = "188********";
$sender["ProvinceName"] = "李先生";
$sender["CityName"] = "深圳市";
$sender["ExpAreaName"] = "福田区";
$sender["Address"] = "赛格广场5401AB";
$receiver = [];
$receiver["Name"] = "李先生";
$receiver["Mobile"] = "188********";
$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);
//$jsonParam = JSON($eorder);//兼容php5.2(含)以下
echo "电子面单接口提交内容:
".$jsonParam;
$jsonResult = submitEOrder($jsonParam);
echo "
电子面单提交结果:
".$jsonResult;
//解析电子面单返回结果
$result = json_decode($jsonResult, true);
echo "
返回码:".$result["ResultCode"];
if($result["ResultCode"] == "100") {
echo "
是否成功:".$result["Success"];
}
else {
echo "
电子面单下单失败";
}
//-------------------------------------------------------------
/**
* Json方式查询订单物流轨迹
*/
function submitEOrder($requestData){
$datas = array(
'EBusinessID' => EBusinessID,
'RequestType' => '1007',
'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);
if($url_info['port']=='')
{
$url_info['port']=80;
}
echo $url_info['port'];
$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)));
}
/************************************************************** *
* 使用特定function对数组中所有元素做处理
* @param string &$array 要处理的字符串
* @param string $function 要执行的函数
* @return boolean $apply_to_keys_also 是否也应用到key上
* @access public
*
*************************************************************/ function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function,
$apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
/************************************************************** *
* 将数组转换为JSON字符串(兼容中文)
* @param array $array 要转换的数组
* @return string 转换得到的json字符串
* @access public
*
*************************************************************/ function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
?>
Json请求
{
"OrderCode": "012657700387",
"ShipperCode": "HTKY",
"PayType": 1,
"ExpType": 1,
"Cost": 1.0,
"OtherCost": 1.0,
"Sender": {
"Company": "LV",
"Name": "Taylor",
"Mobile": "150********",
"ProvinceName": "上海",
"CityName": "上海",
"ExpAreaName": "青浦区",
"Address": "明珠路73号"
},
"Receiver": {
"Company": "GCCUI",
"Name": "Yann",
"Mobile": "150********",
"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": "小心轻放",
"IsReturnPrintTemplate":1
}
Json返回
失败:
{
"EBusinessID": "1237100",
"ResultCode": "105",
"Reason": "订单号已存在,请勿重复操作",
"UniquerRequestNumber":"5e66486b-8fbc-4131-b875-9b13d2ad1354" }
成功:
{
"EBusinessID": "1237100",
"Order": {
"OrderCode": "012657700387",
"ShipperCode": "HTKY",
"LogisticCode": "50002498503427",
"MarkDestination": "京-朝阳(京-1)",
"OriginCode": "200000",
"OriginName": "上海分拨中心",
"PackageCode": "北京"
},
"PrintTemplate":"此处省略打印模板HTML内容", "EstimatedDeliveryTime":"2016-03-06",
"Callback":"调用时传入的Callback",
"Success": true,
"ResultCode": "100",
"Reason": "成功"
}