Sfoglia il codice sorgente

修复:查询物流信息时报错

Yahoo 4 anni fa
parent
commit
2689e965a3
1 ha cambiato i file con 27 aggiunte e 6 eliminazioni
  1. 27 6
      app/common/library/express/Kuaidi100.php

+ 27 - 6
app/common/library/express/Kuaidi100.php

@@ -25,6 +25,9 @@ class Kuaidi100
 {
     use ErrorTrait;
 
+    // 物流跟踪查询API地址
+    const QUERY_URL = 'http://poll.kuaidi100.com/poll/query.do';
+
     // 微信支付配置
     /* @var array $config */
     private $config;
@@ -43,9 +46,9 @@ class Kuaidi100
      * 执行查询
      * @param string $code
      * @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";
@@ -63,16 +66,34 @@ class Kuaidi100
         ];
         $postData['sign'] = strtoupper(md5($postData['param'] . $this->config['key'] . $postData['customer']));
         // 请求快递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);
         // 记录错误信息
         if (isset($express['returnCode']) || !isset($express['data'])) {
             $this->error = $express['message'] ?? '查询失败';
             return false;
         }
-        // 记录缓存, 时效5分钟
-        Cache::set($cacheIndex, $express['data'], 300);
+        // 记录缓存, 时效30分钟
+        Cache::set($cacheIndex, $express['data'], 3000);
         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;
+    }
 }