Usps.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\express;
  13. use think\facade\Cache;
  14. use cores\traits\ErrorTrait;
  15. use app\common\library\helper;
  16. use think\facade\Log;
  17. /**
  18. * 快递USPS模块
  19. * Class Usps
  20. * @package app\common\library\express
  21. */
  22. class Usps
  23. {
  24. use ErrorTrait;
  25. // 物流跟踪查询API地址
  26. //const QUERY_URL = 'http://production.shippingapis.com/ShippingAPI.dll';
  27. const QUERY_URL = 'http://f1.itdida.com/itdida-api/queryTracks?no=';
  28. // 查询轨迹账号
  29. /* @var string $userId */
  30. private $userId = '';
  31. /**
  32. * 构造方法
  33. * @param $userId
  34. */
  35. // public function __construct($userId)
  36. // {
  37. // $this->userId = $userId;
  38. // }
  39. /**
  40. * 执行查询
  41. * @param string $expressNo
  42. * @return string|bool
  43. */
  44. public function query(string $expressNo)
  45. {
  46. // 缓存索引
  47. $cacheIndex = 'express_usps_' . $expressNo;
  48. if ($cacheData = Cache::get($cacheIndex)) {
  49. return $cacheData;
  50. }
  51. // 参数设置
  52. $postDataXML = '<?xml version="1.0" encoding="UTF-8"?><TrackRequest USERID="' . $this->userId . '"><TrackID ID="' . $expressNo . '"></TrackID></TrackRequest>';
  53. // 请求快递100 api
  54. $result = $this->curlPost(self::QUERY_URL, $postDataXML);
  55. if (array_key_exists('HTTP_RAW_POST_DATA', $GLOBALS)) {
  56. $post_data = $GLOBALS['HTTP_RAW_POST_DATA'];//php接收xml文件的唯一方式
  57. $post_obj = SimpleXml_load_String($post_data, 'SimpleXMLElement', LIBXML_NOCDATA);//加载xml
  58. //$username = (string)$post_obj->username;//直接获取xml中username的值
  59. //todo
  60. } else {
  61. return false;
  62. }
  63. $express = helper::jsonDecode($result);
  64. // 记录错误信息
  65. if (isset($express['returnCode']) || !isset($express['data'])) {
  66. $this->error = $express['message'] ?? '查询失败';
  67. return false;
  68. }
  69. // 记录缓存, 时效30分钟
  70. Cache::set($cacheIndex, $express['data'], 3000);
  71. return $express['data'];
  72. }
  73. /**
  74. * curl请求指定url (post)
  75. * @param $url
  76. * @param string $data
  77. * @return bool|string
  78. */
  79. private function curlPost($url, string $data = '')
  80. {
  81. $header[] = "Content-type: text/xml";//设置http报文头text/xml
  82. $ch = curl_init();
  83. curl_setopt($ch, CURLOPT_POST, 1);//1:post方式 0:get方式
  84. curl_setopt($ch, CURLOPT_HEADER, 0);
  85. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  86. curl_setopt($ch, CURLOPT_URL, $url);
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  88. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  89. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  90. $result = curl_exec($ch);
  91. curl_close($ch);
  92. return $result;
  93. }
  94. /**
  95. * 执行查询
  96. * @param string $expressNo
  97. * @return string|bool
  98. */
  99. public function queryF1(string $expressNo)
  100. {
  101. // 缓存索引
  102. $cacheIndex = 'express_usps_' . $expressNo;
  103. if ($cacheData = Cache::get($cacheIndex)) {
  104. return $cacheData;
  105. }
  106. // 请求快递100 api
  107. $result = $this->curlGet(self::QUERY_URL . $expressNo);
  108. $express = helper::jsonDecode($result);
  109. // 记录错误信息
  110. if (!isset($express['data']) || !empty($express['errorMsg'])) {
  111. $this->error = $express['errorMsg'] ?? '查询失败';
  112. return false;
  113. }
  114. // 记录缓存, 时效30分钟
  115. Cache::set($cacheIndex, $express['data'], 3000);
  116. return $express['data'];
  117. }
  118. //查询尾程信息
  119. public function queryTrackingInfo(string $expressNo)
  120. {
  121. // 缓存索引
  122. $cacheIndex = 'express_usps_' . $expressNo;
  123. if ($cacheData = Cache::get($cacheIndex)) {
  124. return $cacheData;
  125. }
  126. $params = ['keHuDanHaoList' => $expressNo, 'type' => 3];
  127. // 请求快递100 api
  128. $url = 'http://f1.itdida.com/itdida-api/trackingInfo' . http_build_query($params);
  129. $result = $this->curlGet(self::QUERY_URL . $expressNo);
  130. $express = helper::jsonDecode($result);
  131. // 记录错误信息
  132. if (!isset($express['data']) || !empty($express['errorMsg'])) {
  133. $this->error = $express['errorMsg'] ?? '查询失败';
  134. return false;
  135. }
  136. // 记录缓存, 时效30分钟
  137. Cache::set($cacheIndex, $express['data'], 3000);
  138. return $express['data'];
  139. }
  140. public function getItdidaToken()
  141. {
  142. $token = Cache::get('ItdidaToken');
  143. if (empty($token)) {
  144. $params = ['username' => '18665946835', 'password' => 'Yh123456'];
  145. $url = 'http://f1.itdida.com/itdida-api/login?' . http_build_query($params);
  146. $header = ['Content-Type: application/x-www-form-urlencode'];
  147. //$res = curl_post($url, [], $header);
  148. $resJson = '{"data":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhODFiMzZkNGYwZDUxMWVlODM1YjViMWQ1MmE0NjcxNSIsImNyZWF0ZWQiOjE3MTM0OTc1MzQ1MDEsInJvbGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0tFSFUifV0sImRvbWFpbiI6ImYxIiwiZXhwIjoxNzEzNjcwMzM0fQ.bMVc3E2pTJFA0wztgizsXpqexD65TH471lRpj92jqgVKx2-q2qqhevUMRmZ73c2TQihDVXvmjlaNp0dPdEgZDg","statusCode":200,"success":true}';
  149. $res = json_decode($resJson, true);
  150. if (isset($res['statusCode']) && $res['statusCode'] == 200 && $res['success']) {
  151. Cache::set('ItdidaToken', $res['data'], 169200);
  152. return $res['data'];
  153. } else {
  154. Log::error('getItdidaToken失败::' . json_encode($res));
  155. return null;
  156. }
  157. } else {
  158. return $token;
  159. }
  160. }
  161. private function curlGet($url)
  162. {
  163. $header[] = "Accept: application/json";
  164. $ch = curl_init();
  165. curl_setopt($ch, CURLOPT_POST, 0);//1:post方式 0:get方式
  166. curl_setopt($ch, CURLOPT_HEADER, 0);
  167. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  168. curl_setopt($ch, CURLOPT_URL, $url);
  169. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  170. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  171. $result = curl_exec($ch);
  172. curl_close($ch);
  173. return $result;
  174. }
  175. }