Usps.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  17. * 快递USPS模块
  18. * Class Usps
  19. * @package app\common\library\express
  20. */
  21. class Usps
  22. {
  23. use ErrorTrait;
  24. // 物流跟踪查询API地址
  25. //const QUERY_URL = 'http://production.shippingapis.com/ShippingAPI.dll';
  26. const QUERY_URL = 'http://f1.itdida.com/itdida-api/queryTracks?no=';
  27. // 查询轨迹账号
  28. /* @var string $userId */
  29. private $userId = '';
  30. /**
  31. * 构造方法
  32. * @param $userId
  33. */
  34. // public function __construct($userId)
  35. // {
  36. // $this->userId = $userId;
  37. // }
  38. /**
  39. * 执行查询
  40. * @param string $expressNo
  41. * @return string|bool
  42. */
  43. public function query(string $expressNo)
  44. {
  45. // 缓存索引
  46. $cacheIndex = 'express_usps_'. $expressNo;
  47. if ($cacheData = Cache::get($cacheIndex)) {
  48. return $cacheData;
  49. }
  50. // 参数设置
  51. $postDataXML = '<?xml version="1.0" encoding="UTF-8"?><TrackRequest USERID="' . $this->userId . '"><TrackID ID="' . $expressNo . '"></TrackID></TrackRequest>';
  52. // 请求快递100 api
  53. $result = $this->curlPost(self::QUERY_URL, $postDataXML);
  54. if(array_key_exists('HTTP_RAW_POST_DATA', $GLOBALS))
  55. {
  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. private function curlGet($url)
  119. {
  120. $header[] = "Accept: application/json";
  121. $ch = curl_init();
  122. curl_setopt($ch, CURLOPT_POST, 0);//1:post方式 0:get方式
  123. curl_setopt($ch, CURLOPT_HEADER, 0);
  124. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  125. curl_setopt($ch, CURLOPT_URL, $url);
  126. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  128. $result = curl_exec($ch);
  129. curl_close($ch);
  130. return $result;
  131. }
  132. }