作者 karlet

feat:修改curl的post方法

@@ -19,15 +19,13 @@ class Curl @@ -19,15 +19,13 @@ class Curl
19 $output = curl_exec($ch); 19 $output = curl_exec($ch);
20 if ($output === false) { 20 if ($output === false) {
21 echo 'Curl error: ' . curl_error($ch); 21 echo 'Curl error: ' . curl_error($ch);
22 - curl_close($ch);  
23 - return json_encode(['code' => '-1', 'msg' => 'curl error: ' . curl_error($ch)]);  
24 } 22 }
25 curl_close($ch); 23 curl_close($ch);
26 return $output; 24 return $output;
27 } 25 }
28 26
29 //post 获取数据 27 //post 获取数据
30 - static public function httpPost($url, $param = [], $header = []) 28 + static public function httpPost($url, $param = [], $header = [], $asBody = false)
31 { 29 {
32 if (empty($url)) { 30 if (empty($url)) {
33 return false; 31 return false;
@@ -37,21 +35,42 @@ class Curl @@ -37,21 +35,42 @@ class Curl
37 $opts = []; 35 $opts = [];
38 $opts[CURLOPT_POST] = 1; 36 $opts[CURLOPT_POST] = 1;
39 $opts[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36"; 37 $opts[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
40 - if (!empty($header) && in_array('Content-Type:application/json', $header)) {  
41 - $opts[CURLOPT_POSTFIELDS] = json_encode($param); 38 +
  39 + // 修改判断逻辑
  40 + $isJson = false;
  41 + foreach ($header as $h) {
  42 + if (stripos($h, 'Content-Type: application/json') !== false) {
  43 + $isJson = true;
  44 + break;
  45 + }
  46 + }
  47 +
  48 + if ($asBody) {
  49 + // form-data格式提交
  50 + $boundary = '----WebKitFormBoundary' . uniqid();
  51 + $body = '';
  52 + foreach ($param as $key => $value) {
  53 + $body .= "--$boundary\r\n";
  54 + $body .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
  55 + $body .= "$value\r\n";
  56 + }
  57 + $body .= "--$boundary--\r\n";
  58 + $opts[CURLOPT_POSTFIELDS] = $body;
  59 + $header[] = "Content-Type: multipart/form-data; boundary=$boundary";
  60 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
42 } else { 61 } else {
43 - $opts[CURLOPT_POSTFIELDS] = http_build_query($param); 62 + $opts[CURLOPT_POSTFIELDS] = $isJson ? json_encode($param) : http_build_query($param);
44 } 63 }
  64 +
45 curl_setopt_array($ch, $opts); 65 curl_setopt_array($ch, $opts);
46 $output = curl_exec($ch); 66 $output = curl_exec($ch);
47 if ($output === false) { 67 if ($output === false) {
48 echo 'Curl error: ' . curl_error($ch); 68 echo 'Curl error: ' . curl_error($ch);
49 - curl_close($ch);  
50 - return json_encode(['code' => '-1', 'msg' => 'curl error: ' . curl_error($ch)]);  
51 } 69 }
52 curl_close($ch); 70 curl_close($ch);
53 return $output; 71 return $output;
54 } 72 }
  73 +
55 //delete 请求 74 //delete 请求
56 static public function httpDelete($url, $param = [], $header = []) 75 static public function httpDelete($url, $param = [], $header = [])
57 { 76 {
@@ -67,9 +86,6 @@ class Curl @@ -67,9 +86,6 @@ class Curl
67 $output = curl_exec($ch); 86 $output = curl_exec($ch);
68 if ($output === false) { 87 if ($output === false) {
69 echo 'Curl error: ' . curl_error($ch); 88 echo 'Curl error: ' . curl_error($ch);
70 - echo 'Curl error: ' . curl_error($ch);  
71 - curl_close($ch);  
72 - return json_encode(['code' => '-1', 'msg' => 'curl error: ' . curl_error($ch)]);  
73 } 89 }
74 curl_close($ch); 90 curl_close($ch);
75 return $output; 91 return $output;