Model_dpd.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. class Model_Dpd extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. }
  6. public function get_data($data)
  7. {
  8. if($data['printcode'] == 'DPD')
  9. {
  10. $PtId = 'ESBRM';
  11. }
  12. else if($data['printcode'] == 'FTFEDEX')
  13. {
  14. $PtId = 'FEDEX-EU';
  15. }
  16. $ItemId = time().rand(10,99);
  17. $url = "http://exorderwebapi.flytcloud.com/api/OrderSyn/ErpUploadOrder";
  18. $UAccount = '37014';
  19. $Password = strtoupper(md5('921657'));
  20. $Token = '39F63970-CA4C-D919-636A-6F703FD55D82';
  21. //创建并预报订单
  22. $json = array(
  23. 'Token' => $Token,
  24. 'UAccount'=>$UAccount,
  25. 'Password'=>$Password,
  26. 'OrderList' => array(
  27. 0 => array(
  28. 'Address1' => $data['address'],
  29. 'Address2' => $data['address2'],
  30. 'ApiOrderId' => $data['number'],
  31. 'City' => $data['city'],
  32. 'CiId' => $data['lb'],
  33. 'County' => $data['province'],
  34. 'PackType' => 3,//包装类型(1:信封,2:文件,3:包裹)(默认包裹)
  35. 'Phone'=>$data['phone'],
  36. 'PtId' => $PtId,//邮政方式简码
  37. 'ReceiverName' => $data['name'],
  38. 'Remark'=> $data['number'],
  39. 'SalesPlatformFlag' => 0,
  40. 'SyncPlatformFlag' => 'flyt.logistics.longying',//第三平台标识
  41. 'Zip' => trim($data['zipcode'],' '),
  42. 'OrderDetailList' => array(0=>array(
  43. 'ItemName'=>$data['sbpm'],
  44. 'OriginalPlatformOrderId'=>$data['orderinfo'],
  45. 'Price'=>$data['dtsbjz'],
  46. 'Quantities'=>$data['ts']
  47. )
  48. ),
  49. 'HaikwanDetialList' => array(0=>array(
  50. 'HwCode'=>'67041100',
  51. 'ItemCnName'=>$data['zwpm'],
  52. 'ItemEnName'=>$data['sbpm'],
  53. 'ProducingArea'=>'CN',
  54. 'Quantities'=>$data['ts'],
  55. 'UnitPrice'=>$data['dtsbjz'],
  56. 'Weight'=>$data['zzl'],
  57. 'CCode'=>'USD',
  58. )
  59. )
  60. )
  61. )
  62. );
  63. $json = json_encode($json,true);
  64. $ch = curl_init();
  65. curl_setopt($ch,CURLOPT_URL,$url);
  66. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  67. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  68. 'Accept-Language: zh-cn',
  69. 'Connection: Keep-Alive',
  70. 'Cache-Control: no-cache',
  71. 'Content-type: application/json;charset=UTF-8'));
  72. curl_setopt($ch,CURLOPT_POST, 1);
  73. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  74. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  75. $res = curl_exec($ch);
  76. curl_close($ch);
  77. $res = json_decode($res,true);
  78. if(!isset($res['ErpSuccessOrders'][0]['TraceId']) && !isset($res['ErpSuccessOrders'][0]['OrderId']))
  79. {
  80. $g = array('x'=>'0','Description'=>$res['Remark'].':'.$res['ErpFailOrders'][0]['Remark'],'res'=>json_encode(array($res),true));
  81. }
  82. else
  83. {
  84. if(!$res['ErpSuccessOrders'][0]['TraceId'])
  85. {
  86. for($i=0;$i<20;$i++)
  87. {
  88. sleep(6);
  89. $ch = curl_init();
  90. curl_setopt($ch,CURLOPT_URL,$url);
  91. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  92. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  93. 'Accept-Language: zh-cn',
  94. 'Connection: Keep-Alive',
  95. 'Cache-Control: no-cache',
  96. 'Content-type: application/json;charset=UTF-8'));
  97. curl_setopt($ch,CURLOPT_POST, 1);
  98. curl_setopt($ch,CURLOPT_TIMEOUT,5);
  99. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  100. $res = curl_exec($ch);
  101. curl_close($ch);
  102. $res = json_decode($res,true);
  103. if($res['ErpSuccessOrders'][0]['TraceId'] != '' || $res['ErpSuccessOrders'][0]['TraceId'] != 'null' || $res['ErpSuccessOrders'][0]['TraceId'] != NULL)
  104. {
  105. break;
  106. }
  107. }
  108. }
  109. /**
  110. //判断是否有运单
  111. $yd = $this->get_yd($res['ErpSuccessOrders'][0]['TraceId']);
  112. if($yd[0] != '2')
  113. {
  114. $g = array('x'=>'1','waybill'=>$res['ErpSuccessOrders'][0]['TraceId'],'yd'=>$yd[1],'res'=>json_encode(array($res,$yd),true));
  115. }
  116. else
  117. {
  118. $g = array('x'=>'0','Description'=>$yd[1],'res'=>json_encode(array($res,$yd),true));
  119. }
  120. **/
  121. if($res['Success'] == 'true')
  122. {
  123. $g = array('x'=>'1','waybill'=>$res['ErpSuccessOrders'][0]['TraceId'],'yd'=>'','res'=>json_encode(array($res),true));
  124. }
  125. else
  126. {
  127. $g = array('x'=>'0','Description'=>$yd[1],'res'=>json_encode(array($res,$yd),true));
  128. }
  129. }
  130. return $g;
  131. }
  132. public function get_token()
  133. {
  134. $url = "http://exapi.flytcloud.com/api/auth/Authorization/GetAccessToken";
  135. $grant_type = 'password';
  136. $username = 'longying';
  137. $password = md5('ljygt&0IP!');
  138. //创建并预报订单
  139. $json = array(
  140. 'grant_type' => $grant_type,
  141. 'username' => $username,
  142. 'password' => $password,
  143. );
  144. $json = json_encode($json,true);
  145. $ch = curl_init();
  146. curl_setopt($ch,CURLOPT_URL,$url);
  147. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  148. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  149. 'Accept-Language: zh-cn',
  150. 'Connection: Keep-Alive',
  151. 'Cache-Control: no-cache',
  152. 'Content-type: application/json;charset=UTF-8'));
  153. curl_setopt($ch,CURLOPT_POST, 1);
  154. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  155. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  156. $res = curl_exec($ch);
  157. curl_close($ch);
  158. $res = json_decode($res,true);
  159. return $res;
  160. }
  161. public function get_yd($data)
  162. {
  163. $path = $_SERVER["DOCUMENT_ROOT"] . '/data/dpd_access_token.txt';
  164. $access_token = '';
  165. if(file_exists($path))
  166. {
  167. $file_read = fopen($path, 'r');
  168. $access_str = fgets($file_read);
  169. fclose($file_read);
  170. $access_arr = explode(':', $access_str);
  171. if (time() < intval($access_arr[0]))
  172. {
  173. $access_token = isset($access_arr[1]) ? $access_arr[1] : '';
  174. }
  175. }
  176. if(!$access_token)
  177. {
  178. $file = fopen($path, 'w');
  179. $info = $this->get_token();
  180. if(!$info['expires_in'])
  181. {
  182. return array(2,'令牌获取错误,请重试!');exit;
  183. }
  184. $access_new = (time() + $info['expires_in']) . ':' . $info['access_token'];
  185. fwrite($file, $access_new);
  186. fclose($file);
  187. $access_token = $info['access_token'];
  188. }
  189. $url = "http://exapi.flytcloud.com/api/label/LabelProvider/GetLabelBatchExt";
  190. $Token = $access_token;
  191. //创建并预报订单
  192. $json = array(
  193. 'OrderIdlst' => array($data),
  194. 'Format' => '0'
  195. );
  196. $json = json_encode($json,true);
  197. $ch = curl_init();
  198. curl_setopt($ch,CURLOPT_URL,$url);
  199. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  200. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  201. 'token:'.$Token,
  202. 'Content-type: application/json;charset=UTF-8'));
  203. curl_setopt($ch,CURLOPT_POST, 1);
  204. curl_setopt($ch,CURLOPT_TIMEOUT,3);
  205. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  206. $res = curl_exec($ch);
  207. curl_close($ch);
  208. $res = json_decode($res,true);
  209. if(isset($res['Data']['Label']))
  210. {
  211. return array(1,$res['Data']['Label']);exit;
  212. }
  213. else
  214. {
  215. return array(2,$res['ErrMsg']);exit;
  216. }
  217. }
  218. public function get_logistics($waybill)
  219. {
  220. $url = "http://tracking.flytcloud.com/Trackingod/TrackOrder";
  221. $Token = '119904B54744499F9C7A29A84C2C7051';
  222. //创建并预报订单
  223. $json = array(
  224. 'key' => $Token,
  225. 'tracking_number' => $waybill,
  226. );
  227. $json = json_encode($json,true);
  228. $ch = curl_init();
  229. curl_setopt($ch,CURLOPT_URL,$url);
  230. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  231. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  232. 'Accept-Language: zh-cn',
  233. 'Connection: Keep-Alive',
  234. 'Cache-Control: no-cache',
  235. 'Content-type: application/json;charset=UTF-8'));
  236. curl_setopt($ch,CURLOPT_POST, 1);
  237. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,600);
  238. curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
  239. $res = curl_exec($ch);
  240. curl_close($ch);
  241. $res = json_decode($res,true);
  242. $list = '';
  243. if(isset($res['dat']['0']['track']['z1'][0]['trackingStatus']))
  244. {
  245. if($res['dat']['0']['track']['e'] > '0')
  246. {
  247. $zt = $res['dat']['0']['track']['e'];
  248. if($zt >= '2' && $zt< '10')
  249. {
  250. $exstate = 1;
  251. }
  252. else if($zt >= '10' && $zt < '30')
  253. {
  254. $exstate = 2;
  255. }
  256. else if($zt >= '30' && $zt < '40')
  257. {
  258. $exstate = 3;
  259. }
  260. else if($zt == '40' || $zt == '41')
  261. {
  262. $exstate = 4;
  263. }
  264. else
  265. {
  266. $exstate = 0;
  267. }
  268. if($se)
  269. {
  270. $list = array('f'=>2);
  271. }
  272. else
  273. {
  274. foreach($res['dat']['0']['track']['z1'] as $v)
  275. {
  276. $xq .= date('m-d H:i',strtotime($v['a'])).' '.$v['z'].'<br />';
  277. }
  278. $content = date('Y-m-d H',time()).'-查询成功';
  279. $list = array('exstate'=>$exstate,'content'=>$content,'f'=>1,'data'=>$xq);//正常可发送站内信
  280. }
  281. }
  282. else
  283. {
  284. $list = '';
  285. }
  286. }
  287. return $res;
  288. }
  289. } //end class