作者 karlet

feat:增加body提交方式

@@ -25,7 +25,7 @@ class Curl @@ -25,7 +25,7 @@ class Curl
25 } 25 }
26 26
27 //post 获取数据 27 //post 获取数据
28 - static public function httpPost($url, $param = [], $header = []) 28 + static public function httpPost($url, $param = [], $header = [], $asBody = false)
29 { 29 {
30 if (empty($url)) { 30 if (empty($url)) {
31 return false; 31 return false;
@@ -45,7 +45,24 @@ class Curl @@ -45,7 +45,24 @@ class Curl
45 } 45 }
46 } 46 }
47 47
48 - $opts[CURLOPT_POSTFIELDS] = $isJson ? json_encode($param) : http_build_query($param); 48 + if ($asBody) {
  49 + // form-data格式提交
  50 + $boundary = '----WebKitFormBoundary' . uniqid();
  51 + $body = '';
  52 + foreach ($param as $key => $value) {
  53 + var_dump($key, $value);
  54 + $body .= "--$boundary\r\n";
  55 + $body .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
  56 + $body .= "$value\r\n";
  57 + }
  58 + var_dump($body);
  59 + $body .= "--$boundary--\r\n";
  60 + $opts[CURLOPT_POSTFIELDS] = $body;
  61 + $header[] = "Content-Type: multipart/form-data; boundary=$boundary";
  62 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  63 + } else {
  64 + $opts[CURLOPT_POSTFIELDS] = $isJson ? json_encode($param) : http_build_query($param);
  65 + }
49 66
50 curl_setopt_array($ch, $opts); 67 curl_setopt_array($ch, $opts);
51 $output = curl_exec($ch); 68 $output = curl_exec($ch);
@@ -55,6 +72,7 @@ class Curl @@ -55,6 +72,7 @@ class Curl
55 curl_close($ch); 72 curl_close($ch);
56 return $output; 73 return $output;
57 } 74 }
  75 +
58 //delete 请求 76 //delete 请求
59 static public function httpDelete($url, $param = [], $header = []) 77 static public function httpDelete($url, $param = [], $header = [])
60 { 78 {