Model_dpd_IOSS.php 8.6 KB

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