浏览代码

设置超时时间

mark 2 年之前
父节点
当前提交
eb7b05e275
共有 1 个文件被更改,包括 41 次插入4 次删除
  1. 41 4
      src/HttpCurl.php

+ 41 - 4
src/HttpCurl.php

@@ -9,6 +9,21 @@ use Tool\MayouTool\Exception\HttpCustomeException;
 
 class HttpCurl
 {
+    /**
+     * @var $curl Curl
+     */
+    static $curl;
+
+    /**
+     * @var int 超时时间
+     */
+    static $timeOut = 180;
+
+    /**
+     * @var int 连接超时时间
+     */
+    static $connectTimeOut = 5;
+
     /**
      * @param string $url 请求地址
      * @param array $params 请求参数
@@ -20,7 +35,13 @@ class HttpCurl
         if(isset($_GET["traceId"])){
             $headers["traceId"] = $_GET["traceId"];
         }
-        $curl = new Curl();
+
+        self::$curl = $curl = new Curl();
+        //设置超时时间
+        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        //设置连接超时时间
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+
         //塞入header头
         foreach ($headers as $key=>$header){
             $curl->setHeader($key,$header);
@@ -48,7 +69,12 @@ class HttpCurl
             $headers["traceId"] = $_GET["traceId"];
         }
 
-        $curl = new Curl();
+        self::$curl = $curl = new Curl();
+        //设置超时时间
+        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        //设置连接超时时间
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+
         //塞入header头
         foreach ($headers as $key=>$header){
             $curl->setHeader($key,$header);
@@ -77,7 +103,13 @@ class HttpCurl
         if(isset($_GET["traceId"])){
             $headers["traceId"] = $_GET["traceId"];
         }
-        $curl = new Curl();
+
+        self::$curl = $curl = new Curl();
+        //设置超时时间
+        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        //设置连接超时时间
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+
         //塞入header头
         foreach ($headers as $key=>$header){
             $curl->setHeader($key,$header);
@@ -105,7 +137,12 @@ class HttpCurl
             $headers["traceId"] = $_GET["traceId"];
         }
 
-        $curl = new Curl();
+        self::$curl = $curl = new Curl();
+        //设置超时时间
+        $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
+        //设置连接超时时间
+        $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
+
         //塞入header头
         foreach ($headers as $key=>$header){
             $curl->setHeader($key,$header);