ExpressHandler.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Handlers;
  3. use GuzzleHttp\Client;
  4. use http\Env\Request;
  5. use Illuminate\Support\Collection;
  6. class ExpressHandler {
  7. public static function getExpressCode(){
  8. $arr=[
  9. 'SF' =>'顺丰速运',
  10. 'HTKY' =>'百世快递',
  11. 'ZTO' =>'中通快递',
  12. 'STO' =>'申通快递',
  13. 'YTO' =>'圆通速递',
  14. 'YD' =>'韵达速递',
  15. 'YZPY' =>'邮政快递包裹',
  16. 'EMS' =>'EMS',
  17. 'HHTT' =>'天天快递',
  18. 'JD' =>'京东快递',
  19. 'UC' =>'优速快递',
  20. 'DBL' =>'德邦快递',
  21. 'ZJS' =>'宅急送',
  22. 'PJ' =>'品骏快递',
  23. 'JTSD' =>'极兔速递',
  24. 'YMDD' =>'壹米滴答',
  25. ];
  26. return $arr;
  27. }
  28. public function search($express_code,$ShipperCode=null,$OrderCode=null,$CustomerName=null){
  29. try{
  30. if ($ShipperCode=='ZTO'){
  31. $client=new Client();
  32. $url='http://japi.zto.cn/traceInterfaceNewTraces';
  33. $array=[
  34. 'headers'=>[
  35. 'ContentType'=>'application/x-www-form-urlencoded',
  36. 'x-companyid'=>'206f48b6bded4906a06eddd39bf26e7c',
  37. 'x-datadigest'=>base64_encode(md5('data=["'.$express_code.'"]&company_id=206f48b6bded4906a06eddd39bf26e7c&msg_type=NEW_TRACES0480ecc2d633',true))
  38. ],
  39. 'query'=>[
  40. 'data'=>'["'.$express_code.'"]',
  41. 'company_id'=>'206f48b6bded4906a06eddd39bf26e7c',
  42. 'msg_type'=>'NEW_TRACES',
  43. ]
  44. ];
  45. $request=$client->request('POST',$url,$array);
  46. $ad=$request->getBody()->getContents();
  47. $re=[];
  48. $state=0;
  49. $ads=json_decode($ad);
  50. $arr=(array)$ads;
  51. if ($arr['data'][0]->traces){
  52. foreach ($arr['data'][0]->traces as $k=>$v){
  53. $re[$k]['AcceptStation']=$v->desc;
  54. $re[$k]['AcceptTime']=strtotime($v->scanDate)*1000;
  55. if ($v->scanType=='ARRIVAL' || $v->scanType=='SIGNED' || $v->scanType=='5' || $v->scanType=='6' || $v->scanType=='签收'){
  56. $state=3;
  57. }
  58. }
  59. }
  60. $a['LogisticCode']=$express_code;
  61. $a['ShipperCode']=$ShipperCode;
  62. $a['Traces']=$re;
  63. $a['State']=$state;
  64. return $a;
  65. }
  66. $client=new Client();
  67. // if ($ShipperCode=='DBL'){
  68. // $url='https://www.deppon.com/gwapi/trackService/eco/track/searchTrack?billNo='.$express_code;
  69. // $request=$client->request('get',$url);
  70. // $ad=$request->getBody()->getContents();
  71. // $ads=json_decode($ad);
  72. // $re=[];
  73. // $state=0;
  74. // if ($ads->status=='success'){
  75. // foreach ($ads->result->tracks as $k=>$v){
  76. // $re[$k]['AcceptStation']=$v->contentNoLinkLabel;
  77. // $re[$k]['AcceptTime']=$v->operateTime;
  78. // }
  79. // if ($ads->result->billNoState=='已签收'){
  80. // $state=3;
  81. // }
  82. // }
  83. // $a['LogisticCode']=$express_code;
  84. // $a['ShipperCode']=$ShipperCode;
  85. // $a['Traces']=$re;
  86. // $a['message']=$ads->message;
  87. // $a['State']=$state;
  88. // return $a;
  89. // }
  90. $appkey='321b74bb-84a3-4b9c-af1d-937583f9a3a0';
  91. $appid=1640111;
  92. $url='http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
  93. $array=[
  94. 'query'=>[
  95. 'RequestData'=>urlencode($this->requestdata($express_code,$ShipperCode,$OrderCode,$CustomerName)),
  96. 'EBusinessID'=>$appid,
  97. 'RequestType'=>'8002',
  98. 'DataSign'=>urlencode($this->encrypt($this->requestdata($express_code,$ShipperCode,$OrderCode,$CustomerName),$appkey)),
  99. 'DataType'=>'2'
  100. ]
  101. ];
  102. $re=$client->request('POST',$url,$array);
  103. $dd=$re->getBody()->getContents();
  104. $d=json_decode($dd);
  105. if ($d->Traces){
  106. foreach ($d->Traces as $k=>$v){
  107. $v->AcceptTime=strtotime($v->AcceptTime)*1000;
  108. }
  109. }
  110. return $d;
  111. }catch (\Exception $exception){
  112. $a['LogisticCode']=$express_code;
  113. $a['ShipperCode']=$ShipperCode;
  114. $a['Traces']=[];
  115. $a['State']=40010;
  116. return $a;
  117. }
  118. }
  119. private function requestdata($express_code,$ShipperCode =null,$OrderCode=null,$CustomerName=null){
  120. if ($ShipperCode){
  121. $requestdata['ShipperCode']=$ShipperCode;
  122. }
  123. if ($OrderCode){
  124. $requestdata['OrderCode']=$OrderCode;
  125. }
  126. if ($CustomerName){
  127. $requestdata['CustomerName']=$CustomerName;
  128. }
  129. $requestdata['LogisticCode']=$express_code;
  130. return json_encode($requestdata);
  131. }
  132. public function encrypt($reqiredata,$appkey){
  133. return base64_encode(md5($reqiredata.$appkey));
  134. }
  135. public function getExpressCompany($LogisticCode){
  136. $client = new Client();
  137. $appKey = '321b74bb-84a3-4b9c-af1d-937583f9a3a0';
  138. $appId = 1640111;
  139. // $url = 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json';
  140. $url = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
  141. $requestData = $this->requestData($LogisticCode);
  142. $array = [
  143. 'query' => [
  144. 'RequestData' => urlencode($requestData),
  145. 'EBusinessID' => $appId,
  146. 'RequestType' => '2002',
  147. 'DataSign' => urlencode($this->encrypt($requestData, $appKey)),
  148. 'DataType' => '2'
  149. ]
  150. ];
  151. $result = $client->request('POST', $url, $array);
  152. $res = json_decode($result->getBody()->getContents());
  153. return $res;
  154. }
  155. }