ExpressHandler.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. ];
  24. return $arr;
  25. }
  26. public function search($express_code,$ShipperCode=null,$OrderCode=null,$CustomerName=null){
  27. try{
  28. if ($ShipperCode=='ZTO'){
  29. $client=new Client();
  30. $url='http://japi.zto.cn/traceInterfaceNewTraces';
  31. $array=[
  32. 'headers'=>[
  33. 'ContentType'=>'application/x-www-form-urlencoded',
  34. 'x-companyid'=>'206f48b6bded4906a06eddd39bf26e7c',
  35. 'x-datadigest'=>base64_encode(md5('data=["'.$express_code.'"]&company_id=206f48b6bded4906a06eddd39bf26e7c&msg_type=NEW_TRACES0480ecc2d633',true))
  36. ],
  37. 'query'=>[
  38. 'data'=>'["'.$express_code.'"]',
  39. 'company_id'=>'206f48b6bded4906a06eddd39bf26e7c',
  40. 'msg_type'=>'NEW_TRACES',
  41. ]
  42. ];
  43. $request=$client->request('POST',$url,$array);
  44. $ad=$request->getBody()->getContents();
  45. $re=[];
  46. $state=0;
  47. $ads=json_decode($ad);
  48. $arr=(array)$ads;
  49. if ($arr['data'][0]->traces){
  50. foreach ($arr['data'][0]->traces as $k=>$v){
  51. $re[$k]['AcceptStation']=$v->desc;
  52. $re[$k]['AcceptTime']=strtotime($v->scanDate)*1000;
  53. if ($v->scanType=='ARRIVAL' || $v->scanType=='SIGNED' || $v->scanType=='5' || $v->scanType=='6' || $v->scanType=='签收'){
  54. $state=3;
  55. }
  56. }
  57. }
  58. $a['LogisticCode']=$express_code;
  59. $a['ShipperCode']=$ShipperCode;
  60. $a['Traces']=$re;
  61. $a['State']=$state;
  62. return $a;
  63. }
  64. $client=new Client();
  65. if ($ShipperCode=='DBL'){
  66. $url='https://www.deppon.com/gwapi/trackService/eco/track/searchTrack?billNo='.$express_code;
  67. $request=$client->request('get',$url);
  68. $ad=$request->getBody()->getContents();
  69. $ads=json_decode($ad);
  70. $re=[];
  71. $state=0;
  72. if ($ads->status=='success'){
  73. foreach ($ads->result->tracks as $k=>$v){
  74. $re[$k]['AcceptStation']=$v->contentNoLinkLabel;
  75. $re[$k]['AcceptTime']=$v->operateTime;
  76. }
  77. if ($ads->result->billNoState=='已签收'){
  78. $state=3;
  79. }
  80. }
  81. $a['LogisticCode']=$express_code;
  82. $a['ShipperCode']=$ShipperCode;
  83. $a['Traces']=$re;
  84. $a['message']=$ads->message;
  85. $a['State']=$state;
  86. return $a;
  87. }
  88. $appkey='5df378bc-fc24-4482-9b30-3fbe99ce609d';
  89. $appid=1731767;
  90. $url='http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
  91. $array=[
  92. 'query'=>[
  93. 'RequestData'=>urlencode($this->requestdata($express_code,$ShipperCode,$OrderCode,$CustomerName)),
  94. 'EBusinessID'=>$appid,
  95. 'RequestType'=>'8001',
  96. 'DataSign'=>urlencode($this->encrypt($this->requestdata($express_code,$ShipperCode,$OrderCode,$CustomerName),$appkey)),
  97. 'DataType'=>'2'
  98. ]
  99. ];
  100. $re=$client->request('POST',$url,$array);
  101. $dd=$re->getBody()->getContents();
  102. $d=json_decode($dd);
  103. if ($d->Traces){
  104. foreach ($d->Traces as $k=>$v){
  105. $v->AcceptTime=strtotime($v->AcceptTime)*1000;
  106. }
  107. }
  108. return $d;
  109. }catch (\Exception $exception){
  110. $a['LogisticCode']=$express_code;
  111. $a['ShipperCode']=$ShipperCode;
  112. $a['Traces']=[];
  113. $a['State']=40010;
  114. return $a;
  115. }
  116. }
  117. private function requestdata($express_code,$ShipperCode =null,$OrderCode=null,$CustomerName=null){
  118. if ($ShipperCode){
  119. $requestdata['ShipperCode']=$ShipperCode;
  120. }
  121. if ($OrderCode){
  122. $requestdata['OrderCode']=$OrderCode;
  123. }
  124. if ($CustomerName){
  125. $requestdata['CustomerName']=$CustomerName;
  126. }
  127. $requestdata['LogisticCode']=$express_code;
  128. return json_encode($requestdata);
  129. }
  130. public function encrypt($reqiredata,$appkey){
  131. return base64_encode(md5($reqiredata.$appkey));
  132. }
  133. public function getExpressCompany($LogisticCode){
  134. $client = new Client();
  135. $appKey = '321b74bb-84a3-4b9c-af1d-937583f9a3a0';
  136. $appId = 1640111;
  137. // $url = 'http://sandboxapi.kdniao.com:8080/kdniaosandbox/gateway/exterfaceInvoke.json';
  138. $url = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
  139. $requestData = $this->requestData($LogisticCode);
  140. $array = [
  141. 'query' => [
  142. 'RequestData' => urlencode($requestData),
  143. 'EBusinessID' => $appId,
  144. 'RequestType' => '2002',
  145. 'DataSign' => urlencode($this->encrypt($requestData, $appKey)),
  146. 'DataType' => '2'
  147. ]
  148. ];
  149. $result = $client->request('POST', $url, $array);
  150. $res = json_decode($result->getBody()->getContents());
  151. return $res;
  152. }
  153. }