圆通 API 接口 - 简单封装 PHP
yto.api.php 文件
class YTO {
private
$_domain = 'http://opentestapi.yto.net.cn/service/'
,$_id
,$_key
,$_secret
,$url
,$values
,$result
,$_mouth = [];
const config = [
'traces' => [
'_id' => "YTOTEST"
,'_key' => "sF1Jzn"
,'_secret' => "1QLlIZ"
]
,'orders' => [
'_id' => "K21000119"
,'_key' => "u2Z1F7Fh"
,'_secret' => null
]
];
function __construct($c,$mouth=[]){
if($c) foreach($c as $k=>$v) if(property_exists($this,$k)) $this->$k=$v;
return self::mouth($mouth);
}
function __set($n,$v){
if($v != '') $this->values[$n] = $v;
}
function __get($name){
return $this->values[$name];
}
public function mouth($arr){
$this->_mouth = array_merge($this->_mouth,$arr);
return $this;
}
private function clear(){
$this->url = $this->values = null;
return $this;
}
public function init($m = 'orders'){
return self::__construct(self::config[$m]);
}
public function orders($data,$open = []){
self::send('orders');
if(!$open) $open = ['id'=>$this->_id,'key' => $this->_key];
$tpl = '{
clientID:'.$open['id'].'
,logisticProviderID:YTO
,customerId:'.$open['id'].'
,txLogisticID:orderid
,orderType:1
,serviceType:0
,flag:1
,sender:sender
,receiver:receiver
}';
$data = self::add($tpl,$data,true);
$data = self::enxml(['RequestOrder'=>$data]);
$open['data'] = $data;
$open['sign'] = base64_encode(md5($data.$open['key'],true));
$tpl = '{type:offline,clientId:id,logistics_interface:data,data_digest:sign}';
return self::add($tpl,$open);
}
public function traces($bill){
$tpl = '{
format:JSON
,user_id:id
,app_key:key
,timestamp:time
,v:1.01
,method:yto.Marketing.WaybillTrace
}';
$data = self::send('traces')->add($tpl,[
'id' => $this->_id
,'key' => $this->_key
,'time' => date('Y-m-d H:i:s')
],true);
ksort($data);
$sing = $this->_secret;
foreach($data as $k=>$v) $sing .= $k.$v;
$this->sign = strtoupper(md5($sing));
return $this->add(['param'=>json_encode([["Number"=>$bill]]) ]);
}