PHP调用快递100电子面单接口API接口
1.注册申请快递100接口
https://poll.kuaidi100.com/pollquery/pollStep.do?method=myApiKey.
申请快递100企业版-快递查询接口(API) 截图如下
申请通过快递100接口会已邮件的形式发送给你对应的配置参数 截图如下
2.接口调用
// 快递100 电子面单key
protected $free_sheetkey = '********';//申请通过快递100提供
protected $free_secret = '******'; //申请通过快递100提供
/*
*电子面单接口API
* @order array() 订单信息 ['out_trade_no'->内部订单号,'num'->数量,'weight'->重量]
* @address array() 收货人信息
* @returnsetting array() 发货人信息
*/
public function getElecOrder($order=array(),$address=array(),$returnsetting=array()){
// $getlists = 'http://api.kuaidi100.com/eorderapi.do?method=getElecOrder';
$param = $this->get_recMan($order,$address,$returnsetting);
$str = $this->setParameter($param);
$http = 'http://api.kuaidi100.com/eorderapi.do?method=getElecOrder&'.$str;
$return_data = http($http);
return $return_data;
}
// 快递100 面单数据实体
/**
* @orderid 订单号
* @$address 收件人地址
* @ returnsetting 寄件地址
**/
public function get_recMan($order=array(),$address=array(),$returnsetting=array()){
$paramlists = array(
'partnerId' => '****', //中通快递电子面单账号 需要跟对应快递合作,顺丰,宅急送可免
'partnerKey' => '****',//中通快递电子面单密码
'kuaidicom' => 'zhongtong', //中通快递
'orderId' => $order['out_trade_no'], //内部订单编号
'recMan' => array( //收件人信息
'name' => $address['consigner'],
'mobile'=> $address['mobile'],
'zipCode'=> $address['zip_code'],
'printAddr'=> $address['address_info'].$address['address'],
),
'sendMan' => array( // 寄件人信息
'name' => $returnsetting['seller_name'],
'mobile' => $returnsetting['seller_mobile'],
'zipCode' => $returnsetting['seller_zipcode'],
'printAddr' => $returnsetting['shop_address'],
),
'count' =>$order['num'],
'weight' => $order['weight'],
);
return json_encode($paramlists);
}
/**
* 生成签名
* @param $parameter
* @return string
*/
protected function getStringToSign($parameter)
{
// var_dump($parameter);die();
$str = '';
foreach ($parameter as $key => $value) {
if (!empty($value)) {
$str .= ($value);
}
}
$signature = strtoupper(md5($str));
return $signature;
}
protected function setParameter($method)
{
$time = $_SERVER['REQUEST_TIME'];
$publicParameter = array(
'param' => $method,
't' => $time,
'key' => $this->free_sheetkey,
'secret' => $this->free_secret,
);
// var_dump($publicParameter);die();
$sign = $this->getStringToSign($publicParameter);
$parameter = array_merge($publicParameter, ['sign' => $sign]);
$str = '';
foreach ($parameter as $key => $value) {
$str .= $key . '=' . urlencode($value) . '&';
}
// var_dump($str);die();
return rtrim($str, '&');
}
/**
* 发送HTTP请求方法,目前只支持CURL发送请求
*
* @param string $url
* 请求URL
* @param array $params
* 请求参数
* @param string $method
* 请求方法GET/POST
* @return array $data 响应数据
*/
function http($url, $timeout = 30, $header = array())
{
if (! function_exists('curl_init')) {
throw new Exception('server not install curl');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if (! empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
list ($header, $data) = explode("\r\n\r\n", $data);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if ($data == false) {
curl_close($ch);
}
@curl_close($ch);
return $data;
}
快递100技术文档:
https://www.kuaidi100.com/openapi/ele_api.shtml
电子面单我是对接的中通快递,联系了一个中通快递网点。网点通过电子面单下添加业务员会相应给一个 账号和商家密码。