PHP对接快递鸟电子面单

电子面单API为用户提供电子面单下单服务,快递鸟连接多家物流公司,一次接入即可对接多家物流公司电子面单下单通道,为订单信息化、标准化提供保障服务。用户接通快递鸟电子面单API,即可直接下单顺丰、EMS、宅急送、邮政快递包裹等七家公司面单,无需申请开通其他服务。

流程

  • 商家请求快递鸟电子面单接口
  • 取得快递物流号以及打印模板

接口地址:

http://api.kdniao.com/api/EOrderService

代码示例:

$url = "http://api.kdniao.com/api/EOrderService";

//随机生成电子面单订单号
$kd_order_sn = 'KDN'.init_order_sn();

//构造电子面单提交信息
$eorder = [];
//物流编码
$eorder["ShipperCode"] = 'JTSD';
$eorder["OrderCode"] = $kd_order_sn;
//支付方式(1-现付,2-到付,3-月结)
$eorder["PayType"] = 1;
//快递类型
$eorder["ExpType"] = 1;
//返回电子面单模板:0-不需要;1-需要
$eorder['IsReturnPrintTemplate'] = 1;
//模板规格
$eorder['TemplateSize'] = 180;

//根据不用快递需要的电子面单客户号字段不一样(详细看文档)
if ($eorder["ShipperCode"] == 'JTSD') {
    //电子面单客户号
    $eorder['CustomerName'] = '';
    //电子面单客户号密码
    $eorder['CustomerPwd'] = '';
    //电子面单客户月结号
    $eorder['MonthCode'] = '';
}

$setting = SettingModel::config();

$sender = [];
//发货人公司
$sender['Company'] = $setting['send_company'] ?? '';
//发货人姓名
$sender["Name"] = $setting['send_username'] ?? '';
//发货人电话
$sender["Mobile"] = $setting['send_phone'] ?? '';
//发货人省
$sender["ProvinceName"] = $setting['send_province'] ?? '';
//发货人市
$sender["CityName"] = $setting['send_city'] ?? '';
发货人区域
$sender["ExpAreaName"] = $setting['send_area'] ?? '';
//发货人详细地址
$sender["Address"] = $setting['send_address'] ?? '';

$receiver = [];
//收件人姓名
$receiver["Name"] = express_str($this->order['consignee']);
//收件人手机号码
$receiver["Mobile"] = $this->order['phone'];
//收件人省
$receiver["ProvinceName"] = $this->order['province'] ?? '';
//收件人市
$receiver["CityName"] = $this->order['city'] ?? '';
//收件人区域
$receiver["ExpAreaName"] = $this->order['country'] ?? '';
//收件人详细地址
$receiver["Address"] = express_str($this->order['address']);

//商品信息
$commodity = [];
foreach ($this->order_goods as $og) {
    $commodityOne = [];
    //商品名
    $commodityOne["GoodsName"] = express_str($og['product_name']);
    //商品数量
    $commodityOne["Goodsquantity"] = $og['product_num'];
    //商品价格
    $commodityOne["GoodsPrice"] = $og['product_price'];
    //商品描述
    $commodityOne["GoodsDesc"] = express_str($og['product_sku']);
    $commodity[] = $commodityOne;
}

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

//调用电子面单
$jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);

//快递鸟商户号
$kd_mch_id = SettingModel::get_setting('kd_mch_id');
//快递鸟商户key
$kd_key = SettingModel::get_setting('kd_key');

$datas = array(
    'EBusinessID' => $kd_mch_id,
    'RequestType' => '1007',
    'RequestData' => urlencode($requestData),
    'DataType' => '2',
);
$datas['DataSign'] = encrypt($requestData, $kd_key);
$result = sendPost($url, $datas);

$result = json_decode($result, true);

return $result;

用到的方法


//请求报文不能出现特殊字符,需要去掉
function express_str($str)
{
    $str = str_replace("'", '', $str);
    $str = str_replace('"', '', $str);
    $str = str_replace("#", '', $str);
    $str = str_replace("&", '', $str);
    $str = str_replace("+", '', $str);
    $str = str_replace("%", '', $str);
    $str = str_replace("<", '', $str);
    $str = str_replace(">", '', $str);
    return $str;
}

//curl post
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;
}

//加密
public function encrypt($data, $appkey)
{
    return urlencode(base64_encode(md5($data.$appkey)));
}

只是简单的示例,我自己都看不懂的,看下官方文档吧~

Posted in PHP