中通/圆通API接口 PHP 简单封装示例

圆通 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]);
		}
		// 封装 start =========================================================
		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]]) ]);
		}
		// 封装 END =========================================================
		
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值