|
@@ -25,6 +25,9 @@ class Kuaidi100
|
|
|
{
|
|
{
|
|
|
use ErrorTrait;
|
|
use ErrorTrait;
|
|
|
|
|
|
|
|
|
|
+ // 物流跟踪查询API地址
|
|
|
|
|
+ const QUERY_URL = 'http://poll.kuaidi100.com/poll/query.do';
|
|
|
|
|
+
|
|
|
// 微信支付配置
|
|
// 微信支付配置
|
|
|
/* @var array $config */
|
|
/* @var array $config */
|
|
|
private $config;
|
|
private $config;
|
|
@@ -43,9 +46,9 @@ class Kuaidi100
|
|
|
* 执行查询
|
|
* 执行查询
|
|
|
* @param string $code
|
|
* @param string $code
|
|
|
* @param string $expressNo
|
|
* @param string $expressNo
|
|
|
- * @return bool
|
|
|
|
|
|
|
+ * @return string|bool
|
|
|
*/
|
|
*/
|
|
|
- public function query(string $code, string $expressNo): bool
|
|
|
|
|
|
|
+ public function query(string $code, string $expressNo)
|
|
|
{
|
|
{
|
|
|
// 缓存索引
|
|
// 缓存索引
|
|
|
$cacheIndex = "express_{$code}_$expressNo";
|
|
$cacheIndex = "express_{$code}_$expressNo";
|
|
@@ -63,16 +66,34 @@ class Kuaidi100
|
|
|
];
|
|
];
|
|
|
$postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
|
|
$postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
|
|
|
// 请求快递100 api
|
|
// 请求快递100 api
|
|
|
- $url = 'http://poll.kuaidi100.com/poll/query.do';
|
|
|
|
|
- $result = curl_post($url, http_build_query($postData));
|
|
|
|
|
|
|
+ $result = $this->curlGet(self::QUERY_URL, $postData);
|
|
|
$express = helper::jsonDecode($result);
|
|
$express = helper::jsonDecode($result);
|
|
|
// 记录错误信息
|
|
// 记录错误信息
|
|
|
if (isset($express['returnCode']) || !isset($express['data'])) {
|
|
if (isset($express['returnCode']) || !isset($express['data'])) {
|
|
|
$this->error = $express['message'] ?? '查询失败';
|
|
$this->error = $express['message'] ?? '查询失败';
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- // 记录缓存, 时效5分钟
|
|
|
|
|
- Cache::set($cacheIndex, $express['data'], 300);
|
|
|
|
|
|
|
+ // 记录缓存, 时效30分钟
|
|
|
|
|
+ Cache::set($cacheIndex, $express['data'], 3000);
|
|
|
return $express['data'];
|
|
return $express['data'];
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * curl请求指定url (post)
|
|
|
|
|
+ * @param $url
|
|
|
|
|
+ * @param array $data
|
|
|
|
|
+ * @return bool|string
|
|
|
|
|
+ */
|
|
|
|
|
+ private function curlGet($url, array $data = [])
|
|
|
|
|
+ {
|
|
|
|
|
+ $ch = curl_init();
|
|
|
|
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
+ $result = curl_exec($ch);
|
|
|
|
|
+ curl_close($ch);
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|