zxhxx 3 years ago
parent
commit
81d1aa66bf
1 changed files with 13 additions and 9 deletions
  1. 13 9
      extend/liuniu/HttpService.php

+ 13 - 9
extend/liuniu/HttpService.php

@@ -65,7 +65,7 @@ class HttpService
      * @param int $timeout 超时秒数
      * @return bool|string
      */
-    public static function request($url, $method = 'get', $data = array(), $header = false, $timeout = 15,$type='array')
+    public static function request($url, $method = 'get', $data = array(), $header = false, $timeout = 15,$json=false)
     {
         self::$status = null;
         self::$curlError = null;
@@ -76,15 +76,13 @@ class HttpService
         //请求方式
         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
         //post请求
+        if($json && is_array($data))
+        {
+            $data = json_encode($data);
+        }
         if ($method == 'POST')
         {
-            if($type=='array') {
-                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
-            }
-            else
-            {
-                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
-            }
+            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         }
         //超时时间
         curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
@@ -95,7 +93,13 @@ class HttpService
         //返回抓取数据
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
         //输出header头信息
-        curl_setopt($curl, CURLOPT_HEADER, true);
+        if($json){ //发送JSON数据
+            curl_setopt($curl, CURLOPT_HEADER, 0);
+            curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($data)));
+        }
+        else {
+            curl_setopt($curl, CURLOPT_HEADER, true);
+        }
         //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
         curl_setopt($curl, CURLINFO_HEADER_OUT, true);
         //https请求