Usps.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // 查询轨迹账号
  27. /* @var string $userId */
  28. private string $userId;
  29. /**
  30. * 构造方法
  31. * @param $userId
  32. */
  33. public function __construct($userId)
  34. {
  35. $this->userId = $userId;
  36. }
  37. /**
  38. * 执行查询
  39. * @param string $expressNo
  40. * @return string|bool
  41. */
  42. public function query(string $expressNo)
  43. {
  44. // 缓存索引
  45. $cacheIndex = 'express_usps_'. $expressNo;
  46. if ($cacheData = Cache::get($cacheIndex)) {
  47. return $cacheData;
  48. }
  49. // 参数设置
  50. $postDataXML = '<?xml version="1.0" encoding="UTF-8"?><TrackRequest USERID="' . $this->userId . '"><TrackID ID="' . $expressNo . '"></TrackID></TrackRequest>';
  51. // 请求快递100 api
  52. $result = $this->curlPost(self::QUERY_URL, $postDataXML);
  53. if(array_key_exists('HTTP_RAW_POST_DATA', $GLOBALS))
  54. {
  55. $post_data = $GLOBALS['HTTP_RAW_POST_DATA'];//php接收xml文件的唯一方式
  56. $post_obj = SimpleXml_load_String($post_data,'SimpleXMLElement',LIBXML_NOCDATA);//加载xml
  57. //$username = (string)$post_obj->username;//直接获取xml中username的值
  58. //todo
  59. }else{
  60. return false;
  61. }
  62. $express = helper::jsonDecode($result);
  63. // 记录错误信息
  64. if (isset($express['returnCode']) || !isset($express['data'])) {
  65. $this->error = $express['message'] ?? '查询失败';
  66. return false;
  67. }
  68. // 记录缓存, 时效30分钟
  69. Cache::set($cacheIndex, $express['data'], 3000);
  70. return $express['data'];
  71. }
  72. /**
  73. * curl请求指定url (post)
  74. * @param $url
  75. * @param string $data
  76. * @return bool|string
  77. */
  78. private function curlPost($url, string $data = '')
  79. {
  80. $header[] = "Content-type: text/xml";//设置http报文头text/xml
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_POST, 1);//1:post方式 0:get方式
  83. curl_setopt($ch, CURLOPT_HEADER, 0);
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  85. curl_setopt($ch, CURLOPT_URL, $url);
  86. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  87. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  88. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  89. $result = curl_exec($ch);
  90. curl_close($ch);
  91. return $result;
  92. }
  93. }