Model_apitt.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. class Model_apitt extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->_model("Model_logic_ding","logic_ding");
  6. }
  7. public function get_list($shop,$from,$to,$order_status,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)//获取订单列表
  8. {
  9. $url = 'https://open-api.tiktokglobalshop.com';
  10. $link = '/order/202309/orders/search';
  11. $time = time();
  12. $post['create_time_ge'] = $from;
  13. $post['create_time_lt'] = $to;
  14. $post['order_status'] = $order_status;
  15. $queryParams['page_size'] = 100;
  16. $queryParams['sort_order'] = 'ASC';
  17. $queryParams['sort_field'] = 'create_time';
  18. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  19. $queryParams['shop_id'] =$shop['codeid'];
  20. $queryParams['app_key'] =$shop['app_key'];
  21. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  22. $queryParams['timestamp'] =$time;
  23. $link .= '?'.http_build_query($queryParams);
  24. $sign = $this->sign($link,$shop['app_secret'],$post);
  25. $url .= $link.'&sign='.$sign;
  26. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  27. $res = $this->su_curl($post,$url,$headers);
  28. //$this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单".$res);
  29. $res = json_decode($res,true);
  30. $list = array();$page_token = '';$arr = array();
  31. if(isset($res['data']['orders']))
  32. {
  33. foreach ($res['data']['orders'] as $val)
  34. {
  35. $d = $this->fullordertt->get_orderinfo($val['id']);
  36. if(!$d)
  37. {
  38. $list[] = $val['id'];
  39. }
  40. }
  41. if($res['data']['total_count'] > 100)
  42. {
  43. $page_token = $res['data']['next_page_token'];
  44. $synum = $res['data']['total_count']-100;
  45. if($synum > 100)
  46. {
  47. $synum = ceil($synum/100);
  48. }
  49. else
  50. {
  51. $synum = 1;
  52. }
  53. for($i=0;$i<$synum;$i++)
  54. {
  55. if($page_token)
  56. {
  57. $net_date = $this->get_next_list($shop,$from,$to,$order_status,$page_token);
  58. $page_token = $net_date[1];
  59. foreach ($net_date['0'] as $val)
  60. {
  61. $d = $this->fullordertt->get_orderinfo($val['id']);
  62. if(!$d)
  63. {
  64. $list[] = $val['id'];
  65. }
  66. }
  67. }
  68. }
  69. }
  70. $cs = array();
  71. if(count($list) > 50)
  72. {
  73. $n = ceil(count($list)/50);$x = 0;
  74. for($i=0;$i<$n;$i++)
  75. {
  76. $k = ($i<1)?0:$i*50;
  77. $data = array_slice($list,$k,50);
  78. $d = $this->get_data($data,$shop);
  79. $cs[] = $d;
  80. if(isset($d['data']))
  81. {
  82. foreach ($d['data']['orders'] as $val)
  83. {
  84. $arr[$x] = $val;
  85. $x++;
  86. }
  87. }
  88. }
  89. }
  90. else
  91. {
  92. $d = $this->get_data($list,$shop);
  93. if(isset($d['data']))
  94. {
  95. foreach ($d['data']['orders'] as $val)
  96. {
  97. $arr[] = $val;
  98. }
  99. }
  100. }
  101. }
  102. $read = array();
  103. $gnum = array();
  104. foreach ($arr as $val)
  105. {
  106. $create_time = substr($val['create_time'],0,10)-$sjc;
  107. if(isset($gnum[date('ymd',$create_time)]))
  108. {
  109. $gnum[date('ymd',$create_time)] += 1;
  110. // if($shop['id'] == 44){
  111. // $this->logic_ding->sendToDing("测试获取TT订单-1-".date('ymd',$create_time)."-".$gnum[date('ymd',$create_time)]);
  112. // }
  113. }
  114. else
  115. {
  116. //$num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');
  117. //避免number 冲突的问题
  118. $info = $this->fullordertt->find('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"',"*","id desc");
  119. if(empty($info)){
  120. $num = 0;
  121. }else{
  122. $tmp_number_arr = explode("-",$info['number']);
  123. //合并订单
  124. if(count($tmp_number_arr) == 5){
  125. $num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');
  126. }else{
  127. $num = array_pop($tmp_number_arr);
  128. $num = $num*1;
  129. }
  130. }
  131. $gnum[date('ymd',$create_time)] = $num+1;
  132. // if($shop['id'] == 44){
  133. // $this->logic_ding->sendToDing("测试获取TT订单-2-".date('ymd',$create_time)."-".$gnum[date('ymd',$create_time)]);
  134. // }
  135. }
  136. $read[] = $this->get_read($val,$time,$shop,$cuy,$gnum[date('ymd',$create_time)],$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku);
  137. }
  138. return array($read,$list);
  139. }
  140. public function get_next_list($shop,$from,$to,$order_status,$page_token='')//获取订单列表
  141. {
  142. $url = 'https://open-api.tiktokglobalshop.com';
  143. $link = '/order/202309/orders/search';
  144. $time = time();
  145. $post['create_time_ge'] = $from;
  146. $post['create_time_lt'] = $to;
  147. $post['order_status'] = $order_status;
  148. $queryParams['page_size'] = 100;
  149. $queryParams['sort_order'] = 'ASC';
  150. $queryParams['sort_field'] = 'create_time';
  151. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  152. $queryParams['shop_id'] =$shop['codeid'];
  153. $queryParams['app_key'] =$shop['app_key'];
  154. $queryParams['timestamp'] =$time;
  155. if($page_token)
  156. {
  157. $queryParams['page_token'] = $page_token;
  158. }
  159. $link .= '?'.http_build_query($queryParams);
  160. $sign = $this->sign($link,$shop['app_secret'],$post);
  161. $url .= $link.'&sign='.$sign;
  162. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  163. $res = $this->su_curl($post,$url,$headers);
  164. $res = json_decode($res,true);
  165. $orders = array();
  166. if(isset($res['data']['orders']))
  167. {
  168. $orders = $res['data']['orders'];
  169. }
  170. return array($orders,$res['data']['next_page_token']);
  171. }
  172. public function get_data($list,$shop)
  173. {
  174. if(empty($list)){
  175. return [];
  176. }
  177. $url = 'https://open-api.tiktokglobalshop.com';
  178. $link = '/order/202309/orders';
  179. $time = time();
  180. $ids = implode(",",$list);
  181. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$ids;
  182. $sign = $this->sign($link,$shop['app_secret'],'');
  183. $url .= $link.'&sign='.$sign;
  184. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  185. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  186. $res = json_decode($res,true);
  187. return $res;
  188. }
  189. public function get_je($d,$shop)//预估到账金额
  190. {
  191. $url = 'https://open-api.tiktokglobalshop.com';
  192. $link = '/finance/202309/orders/'.$d['orderinfo'].'/statement_transactions';
  193. $time = time();
  194. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time;
  195. $sign = $this->sign($link,$shop['app_secret'],'');
  196. $url .= $link.'&sign='.$sign;
  197. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  198. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  199. $res = json_decode($res,true);
  200. return $res;
  201. }
  202. public function get_read($data,$time,$shop,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)
  203. {
  204. $purl = 'https://shop.tiktok.com/view/product/';
  205. $link = '';$product = '';$sku = '';$quantity = '';$price = "";$row_total ="";
  206. $lpsq = array();
  207. $extra_text = [];
  208. $line_items = [];
  209. $extra_text['packages'] = $data['packages'];
  210. foreach ($data['line_items'] as $v)
  211. {
  212. $line_items[] = [
  213. "id"=>$v["id"],
  214. "package_id"=>$v['package_id'],
  215. 'product_id'=>$v['product_id']
  216. ];
  217. if(isset($lpsq[$v['seller_sku']]))
  218. {
  219. $lpsq[$v['seller_sku']]['quantity'] += 1;
  220. $tmp_row_total = $v['sale_price'] ;
  221. if(empty($v['item_tax'])){
  222. }else{
  223. foreach($v['item_tax'] as $kk => $vv){
  224. if(isset($vv['tax_amount'])){
  225. $tmp_row_total = $tmp_row_total*1 + $vv['tax_amount']*1;
  226. }
  227. }
  228. }
  229. $lpsq[$v['seller_sku']]['row_total'] = $lpsq[$v['seller_sku']]['row_total']*1 + $tmp_row_total*1;
  230. }
  231. else
  232. {
  233. //$lpsq[$v['seller_sku']] = array('link'=>$purl.$v['product_id'],'product'=>$v['product_name'],'sku'=>$v['seller_sku'],'quantity'=>1);
  234. $tmp_row_total = $v['sale_price'] ;
  235. if(empty($v['item_tax'])){
  236. }else{
  237. foreach($v['item_tax'] as $kk => $vv){
  238. if(isset($vv['tax_amount'])){
  239. $tmp_row_total = $tmp_row_total*1 + $vv['tax_amount']*1;
  240. }
  241. }
  242. }
  243. $lpsq[$v['seller_sku']] = array('link'=>$purl.$v['product_id'],'product'=>$v['product_name'],'sku'=>$v['seller_sku'],'quantity'=>1,'price'=>$v['original_price'],"row_total"=>$tmp_row_total);
  244. }
  245. }
  246. $extra_text['line_items'] = $line_items;
  247. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单".json_encode($data));
  248. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单数据转化".json_encode($lpsq));
  249. foreach ($lpsq as $v)
  250. {
  251. $link .= $v['link'].',';
  252. $product .= $v['product'].',';
  253. $sku .= $v['sku'].',';
  254. $quantity .= $v['quantity'].';';
  255. $price .= $v['price'].',';
  256. $row_total .= $v['row_total'].',';
  257. }
  258. if($data['status'] == 'DELIVERED' || $data['status'] == 'IN_TRANSIT' || $data['status'] == 'COMPLETED')
  259. {
  260. $s = 216;
  261. }
  262. else if($data['status'] == 'AWAITING_SHIPMENT')
  263. {
  264. $s = 207;
  265. }
  266. else if($data['status'] == 'CANCELLED')
  267. {
  268. $s = 217;
  269. }
  270. else
  271. {
  272. $s = 283;
  273. }
  274. if(isset($zjsku[$shop['id']]))//判断是否找到店铺SKU
  275. {
  276. $sku = rtrim($sku,',').','.$zjsku[$shop['id']];
  277. }
  278. $post['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  279. $post['user_id'] = $data['user_id'];
  280. $post['state'] = $s;
  281. $post['shop'] = $shop['id'];//店铺ID
  282. $post['user'] = $shop['shopuser'];//店铺负责人
  283. $post['product'] = rtrim($product,',');//产品名称
  284. $post['link'] = rtrim($link,',');//产品链接
  285. $post['issku'] = rtrim($sku,',');
  286. $post['quantity'] = rtrim($quantity,';');
  287. $post['clientremarks'] = $data['buyer_message'];//客户备注
  288. $post['paypal'] = '';//交易号
  289. $post['guarantee'] = '';//卖家保障
  290. $post['parameter'] = '';//属性
  291. $post['shippingmethod'] = $data['delivery_option_name'].' '.$data['payment']['shipping_fee'];//用户选择快递方式及运费金额
  292. $post['source'] = 2;//订单类型
  293. $post['type'] = 1;//发货仓库
  294. $post['capital'] = 3;//资金支付状况,全部付款
  295. $numbernew = $shop['shortname'].'-'.date('ymd',substr($data['create_time'],0,10)-$sjc);
  296. $numberwb = (substr(strval($num+1000),1,3));
  297. if($numbernew == 'TT-QQS-40807')
  298. {
  299. $numberwb = $numberwb+7;
  300. }
  301. //过完今天就删除
  302. $tmp_time = date("Y-m-d");
  303. // if($tmp_time == '20250114'){
  304. // $numberwb = $numberwb+7;
  305. // }
  306. $post['number'] = $shop['shortname'].'-'.date('ymd',substr($data['create_time'],0,10)-$sjc).'-'.$numberwb;//编号
  307. $post['orderinfo'] = $data['id'];//订单号
  308. $post['insurance'] = 0;//运输保险费用
  309. $address = $data['recipient_address']['address_detail'];
  310. if(isset($data['recipient_address']['district_info'][3])){
  311. $post['baddress'] = $address.','.$data['recipient_address']['district_info'][3]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  312. $post['saddress'] = $address.','.$data['recipient_address']['district_info'][3]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  313. }else{
  314. $post['baddress'] = $address.','.$data['recipient_address']['district_info'][2]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  315. $post['saddress'] = $address.','.$data['recipient_address']['district_info'][2]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  316. }
  317. $post['country'] = $cuy[$data['recipient_address']['region_code']];//此国家的ID
  318. $post['al'] = $data['recipient_address']['region_code'];//联邦名称
  319. $post['bname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  320. $post['sname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  321. $post['client'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//公司名称
  322. $post['name'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//收件人名称
  323. $post['phone'] = preg_replace('/\D/s','',$data['recipient_address']['phone_number']);//收件人电话
  324. $post['email'] = preg_replace('/( | | |\s)+/','',$data['buyer_email']);//客户邮箱
  325. $post['zipcode'] = $data['recipient_address']['postal_code'];//收件人邮编
  326. $post['province'] = $data['recipient_address']['district_info'][1]['address_name'];//收件人省份
  327. if(isset($data['recipient_address']['district_info'][3])){
  328. $post['city'] = $data['recipient_address']['district_info'][3]['address_name'];//收件人城市
  329. }else{
  330. $post['city'] = $data['recipient_address']['district_info'][2]['address_name'];//收件人城市
  331. }
  332. $post['address'] = $address;//收件人地址
  333. $post['shouldmoney'] = $data['payment']['total_amount'];//应收金额
  334. $post['skje'] = $post['shouldmoney'];//收款金额
  335. $post['ismoney'] = $data['payment']['currency'].$post['shouldmoney'];//支付币种金额
  336. $yga = $post['shouldmoney'];//金额
  337. $post['cf'] = ($data['shipping_type']=='SELLER')?0:1;//是否仓发TIKTOK平台仓发 SELLER商家发,ERP中1平台仓发,0商家发
  338. $post['budget'] = 0;//预估到账金额
  339. /**
  340. if(isset($data['pay']) && $yga > 0)
  341. {
  342. $post['pay'] = $pay[$data['pay']]['id'];
  343. $ygc = $pay[$data['pay']]['estimaterate'];//预估到账公式
  344. $post['estimaterate'] = $pay[$data['pay']]['estimaterate'];
  345. $ifbudget = eval("return $yga*1.$ygc;");
  346. if($ifbudget > 0)
  347. {
  348. $post['budget'] = eval("return $yga*1.$ygc;");//预估到帐金额
  349. }
  350. }
  351. else if($yga > 0)
  352. {
  353. $post['pay'] = 0;
  354. $ygb = $shop['estimaterate'];//店铺默认到账公式
  355. $post['estimaterate'] = $shop['estimaterate'];
  356. $ifbudget = eval("return $yga*1.$ygb;");
  357. if($ifbudget > 0)
  358. {
  359. $post['budget'] = eval("return $yga*1.$ygb;");//预估到帐金额
  360. }
  361. }
  362. **/
  363. $post['overtime'] = $data['shipping_due_time']-$sjc;//超时时间
  364. $post['buytime'] = substr($data['paid_time'],0,10)-$sjc;//格式化付款时间
  365. $post['dtime'] = substr($data['create_time'],0,10)-$sjc;//订单时间
  366. $post['gtime'] = date('Ymd',substr($data['create_time'],0,10)-$sjc);//格式化订单时间
  367. $post['currencytitle'] = $data['payment']['currency'];//结算币种名称
  368. $post['currency'] = $typeclass[$data['payment']['currency']];//钱币ID
  369. $post['time'] = time();//同步时间
  370. $post['hl'] = '6.4';//汇率
  371. $post['delivery_option_id'] = $data['delivery_option_id'];
  372. if(isset($data['delivery_option_name']))
  373. {
  374. $post['express'] = (isset($ex[$data['delivery_option_name']]))?$ex[$data['delivery_option_name']]['id']:0;//快递方式
  375. }
  376. if(isset($data['line_items'][0]['tracking_number']))
  377. {
  378. $post['waybill'] = $data['line_items'][0]['tracking_number'];
  379. }
  380. //$this->logic_ding->sendToDing("测试获取TT订单编码".$post['number']);
  381. $matching = $this->api->matching($post['quantity'],$post['issku'],$dtc,$money,$int,$dtctitle,$shop,[
  382. 'price'=>trim($price,','),
  383. 'row_total'=>trim($row_total,','),
  384. ]);
  385. $post['shipremarks'] = $matching['title'];
  386. $post['cost'] = $matching['cost'];
  387. $post['purchase'] = $matching['purchase'];
  388. $post['fpdata'] = $matching['product'];
  389. $post['whlabel'] = $matching['whlabel'];
  390. $post['slpx'] = $matching['wcslpx'];
  391. return $post;
  392. }
  393. public function getExpressCompany($shop,$info){
  394. $url = 'https://open-api.tiktokglobalshop.com';
  395. $link = "/fulfillment/202309/orders/".trim($info['orderinfo'])."/shipping_services/query";
  396. $time = time();
  397. $extra_text = json_decode($info['extra_text'],true);
  398. $order_line_item_ids = [];
  399. foreach($extra_text['line_items'] as $v)
  400. {
  401. $order_line_item_ids[] = $v['id'];
  402. }
  403. $post = [
  404. "order_line_item_ids"=>$order_line_item_ids,
  405. "weight"=>[
  406. "value"=>"400",
  407. "unit"=>"GRAM"
  408. ],
  409. "dimensions"=>[
  410. "length"=> "15",
  411. "width"=> "10",
  412. "height"=> "5",
  413. "unit"=> "CM"
  414. ]
  415. ];
  416. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  417. $queryParams['app_key'] =$shop['app_key'];
  418. $queryParams['timestamp'] =$time;
  419. $link .= '?'.http_build_query($queryParams);
  420. $sign = $this->sign($link,$shop['app_secret'],$post);
  421. $url .= $link.'&sign='.$sign;
  422. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  423. $res = $this->su_curl($post,$url,$headers);
  424. $this->logic_ding->sendToDing($info['number']."获取TT快递服务信息".$res);
  425. $res = json_decode($res,true);
  426. return $res;
  427. }
  428. //创建运单
  429. public function createLabel($info,$shop){
  430. $url = 'https://open-api.tiktokglobalshop.com';
  431. $link = "/fulfillment/202309/packages";
  432. $time = time();
  433. $extra_text = json_decode($info['extra_text'],true);
  434. //快递服务商
  435. $server_info = $extra_text['server_info'];
  436. $post = [
  437. 'order_id'=>$server_info['order_id'],
  438. "order_line_item_ids"=>$server_info['order_line_id'],
  439. "dimension"=>$server_info['dimension'],
  440. "shipping_service_id"=>$server_info['shipping_services'][0]['shipping_service_id'],
  441. "weight"=>$server_info['weight'],
  442. ];
  443. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  444. $queryParams['app_key'] =$shop['app_key'];
  445. $queryParams['timestamp'] =$time;
  446. $link .= '?'.http_build_query($queryParams);
  447. $sign = $this->sign($link,$shop['app_secret'],$post);
  448. $url .= $link.'&sign='.$sign;
  449. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  450. $res = $this->su_curl($post,$url,$headers);
  451. $this->logic_ding->sendToDing($info['number']."获取TT快递服务信息".$res);
  452. $res = json_decode($res,true);
  453. return $res;
  454. }
  455. //下载快递面单
  456. public function downloadLabel($package_id,$shop,$info){
  457. $url = 'https://open-api.tiktokglobalshop.com';
  458. $link = '/fulfillment/202309/packages/'.$package_id.'/shipping_documents';
  459. $time = time();
  460. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time."&document_type=SHIPPING_LABEL";
  461. $sign = $this->sign($link,$shop['app_secret'],'');
  462. $url .= $link.'&sign='.$sign;
  463. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  464. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  465. $this->logic_ding->sendToDing($info['number']."获取TT快递服务信息".$res);
  466. $res = json_decode($res,true);
  467. return $res;
  468. }
  469. public function sign($url,$appSecret,$body='')
  470. {
  471. // 解析URL获取查询参数
  472. $queryParams = array();
  473. parse_str(parse_url($url, PHP_URL_QUERY), $queryParams);
  474. // 排除'sign'和'access_token'
  475. unset($queryParams['sign'], $queryParams['access_token']);
  476. // 按字典顺序排序查询参数
  477. ksort($queryParams);
  478. // 拼接参数为{key}{value}格式
  479. $input = '';
  480. foreach ($queryParams as $key => $value)
  481. {
  482. $input .= $key . $value;
  483. }
  484. // 追加请求路径
  485. $path = parse_url($url, PHP_URL_PATH);
  486. $input = $path . $input;
  487. if ($body)
  488. {
  489. $input .= json_encode($body);
  490. }
  491. // 使用app_secret包裹生成的字符串
  492. $input = $appSecret . $input . $appSecret;
  493. // 生成签名
  494. $sign = hash_hmac('sha256', $input,$appSecret);
  495. return $sign;
  496. }
  497. public function su_curl($data,$url,$header=array(),$date_type='json',$timeout=300,$httptype="POST",$userpwd='')
  498. {
  499. if ($date_type == 'http_build_query')
  500. {
  501. $data = http_build_query($data);
  502. }
  503. else if ($date_type == 'json')
  504. {
  505. $data = json_encode($data);
  506. }
  507. $ch = curl_init();
  508. curl_setopt($ch, CURLOPT_URL, $url);
  509. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  510. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  511. curl_setopt($ch, CURLOPT_HEADER, false);
  512. switch ($httptype)
  513. {
  514. case "GET":
  515. curl_setopt($ch, CURLOPT_HTTPGET, true);
  516. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  517. break;
  518. case "POST":
  519. curl_setopt($ch, CURLOPT_POST, true);
  520. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  521. break;
  522. case "PUT":
  523. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  524. break;
  525. case "DELETE":
  526. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  527. break;
  528. }
  529. $isSecure = strpos($url, "https://");
  530. if ($isSecure === 0)
  531. {
  532. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  533. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  534. }
  535. if(!empty($header))
  536. {
  537. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  538. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  539. }
  540. if(!empty($userpwd))
  541. {
  542. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  543. }
  544. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  545. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  546. $result = curl_exec($ch);
  547. curl_close($ch);
  548. return $result;
  549. }
  550. } //end class