123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\library\express;
- use think\facade\Cache;
- use cores\traits\ErrorTrait;
- use app\common\library\helper;
- use think\facade\Log;
- /**
- * 快递USPS模块
- * Class Usps
- * @package app\common\library\express
- */
- class Usps
- {
- use ErrorTrait;
- // 物流跟踪查询API地址
- //const QUERY_URL = 'http://production.shippingapis.com/ShippingAPI.dll';
- const QUERY_URL = 'http://f1.itdida.com/itdida-api/queryTracks?no=';
- // 查询轨迹账号
- /* @var string $userId */
- private $userId = '';
- /**
- * 构造方法
- * @param $userId
- */
- // public function __construct($userId)
- // {
- // $this->userId = $userId;
- // }
- /**
- * 执行查询
- * @param string $expressNo
- * @return string|bool
- */
- public function query(string $expressNo)
- {
- // 缓存索引
- $cacheIndex = 'express_usps_' . $expressNo;
- if ($cacheData = Cache::get($cacheIndex)) {
- return $cacheData;
- }
- // 参数设置
- $postDataXML = '<?xml version="1.0" encoding="UTF-8"?><TrackRequest USERID="' . $this->userId . '"><TrackID ID="' . $expressNo . '"></TrackID></TrackRequest>';
- // 请求快递100 api
- $result = $this->curlPost(self::QUERY_URL, $postDataXML);
- if (array_key_exists('HTTP_RAW_POST_DATA', $GLOBALS)) {
- $post_data = $GLOBALS['HTTP_RAW_POST_DATA'];//php接收xml文件的唯一方式
- $post_obj = SimpleXml_load_String($post_data, 'SimpleXMLElement', LIBXML_NOCDATA);//加载xml
- //$username = (string)$post_obj->username;//直接获取xml中username的值
- //todo
- } else {
- return false;
- }
- $express = helper::jsonDecode($result);
- // 记录错误信息
- if (isset($express['returnCode']) || !isset($express['data'])) {
- $this->error = $express['message'] ?? '查询失败';
- return false;
- }
- // 记录缓存, 时效30分钟
- Cache::set($cacheIndex, $express['data'], 3000);
- return $express['data'];
- }
- /**
- * curl请求指定url (post)
- * @param $url
- * @param string $data
- * @return bool|string
- */
- private function curlPost($url, string $data = '')
- {
- $header[] = "Content-type: text/xml";//设置http报文头text/xml
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);//1:post方式 0:get方式
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- /**
- * 执行查询
- * @param string $expressNo
- * @return string|bool
- */
- public function queryF1(string $expressNo)
- {
- // 缓存索引
- $cacheIndex = 'express_usps_' . $expressNo;
- if ($cacheData = Cache::get($cacheIndex)) {
- return $cacheData;
- }
- // 请求快递100 api
- $result = $this->curlGet(self::QUERY_URL . $expressNo);
- $express = helper::jsonDecode($result);
- // 记录错误信息
- if (!isset($express['data']) || !empty($express['errorMsg'])) {
- $this->error = $express['errorMsg'] ?? '查询失败';
- return false;
- }
- // 记录缓存, 时效30分钟
- Cache::set($cacheIndex, $express['data'], 3000);
- return $express['data'];
- }
- //查询尾程信息
- public function queryTrackingInfo(string $expressNo)
- {
- // 缓存索引
- $cacheIndex = 'express_usps_' . $expressNo;
- if ($cacheData = Cache::get($cacheIndex)) {
- return $cacheData;
- }
- $params = ['keHuDanHaoList' => $expressNo, 'type' => 3];
- // 请求快递100 api
- $url = 'http://f1.itdida.com/itdida-api/trackingInfo' . http_build_query($params);
- $result = $this->curlGet(self::QUERY_URL . $expressNo);
- $express = helper::jsonDecode($result);
- // 记录错误信息
- if (!isset($express['data']) || !empty($express['errorMsg'])) {
- $this->error = $express['errorMsg'] ?? '查询失败';
- return false;
- }
- // 记录缓存, 时效30分钟
- Cache::set($cacheIndex, $express['data'], 3000);
- return $express['data'];
- }
- public function getItdidaToken()
- {
- $token = Cache::get('ItdidaToken');
- if (empty($token)) {
- $params = ['username' => '18665946835', 'password' => 'Yh123456'];
- $url = 'http://f1.itdida.com/itdida-api/login?' . http_build_query($params);
- $header = ['Content-Type: application/x-www-form-urlencode'];
- //$res = curl_post($url, [], $header);
- $resJson = '{"data":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhODFiMzZkNGYwZDUxMWVlODM1YjViMWQ1MmE0NjcxNSIsImNyZWF0ZWQiOjE3MTM0OTc1MzQ1MDEsInJvbGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0tFSFUifV0sImRvbWFpbiI6ImYxIiwiZXhwIjoxNzEzNjcwMzM0fQ.bMVc3E2pTJFA0wztgizsXpqexD65TH471lRpj92jqgVKx2-q2qqhevUMRmZ73c2TQihDVXvmjlaNp0dPdEgZDg","statusCode":200,"success":true}';
- $res = json_decode($resJson, true);
- if (isset($res['statusCode']) && $res['statusCode'] == 200 && $res['success']) {
- Cache::set('ItdidaToken', $res['data'], 169200);
- return $res['data'];
- } else {
- Log::error('getItdidaToken失败::' . json_encode($res));
- return null;
- }
- } else {
- return $token;
- }
- }
- private function curlGet($url)
- {
- $header[] = "Accept: application/json";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 0);//1:post方式 0:get方式
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- }
|