Browse Source

设置超时时间

mark 2 years ago
parent
commit
eb7b05e275
1 changed files with 41 additions and 4 deletions
  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);