Commit 7a35ffa9 authored by 王源's avatar 王源 🎧

优化

parent 914ea9bf
...@@ -19,13 +19,14 @@ namespace Meibuyu\Micro\Tools; ...@@ -19,13 +19,14 @@ namespace Meibuyu\Micro\Tools;
class HttpRequestJson class HttpRequestJson
{ {
protected static function prepareRequest(&$headers = [], &$data = []) protected static function prepareRequest($headers, $data = [])
{ {
$data = json_encode($data); $data = json_encode($data);
if (!$data) { if (!empty($data)) {
$headers['Content-type'] = 'application/json'; $headers['Content-type'] = 'application/json';
$headers['Content-Length'] = strlen($data); $headers['Content-Length'] = strlen($data);
} }
return [$headers, $data];
} }
// /** // /**
...@@ -43,27 +44,27 @@ class HttpRequestJson ...@@ -43,27 +44,27 @@ class HttpRequestJson
// /** // /**
// * post请求 // * post请求
// * @param $url // * @param $url
// * @param $dataArray // * @param $data
// * @param array $httpHeaders // * @param array $httpHeaders
// * @return ResponseInterface // * @return ResponseInterface
// */ // */
// public static function post($url, $dataArray, $httpHeaders = []) // public static function post($url, $data, $httpHeaders = [])
// { // {
// $client = new Client(['timeout' => 30]); // $client = new Client(['timeout' => 30]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $dataArray]); // return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
// } // }
// //
// /** // /**
// * put请求 // * put请求
// * @param $url // * @param $url
// * @param $dataArray // * @param $data
// * @param array $httpHeaders // * @param array $httpHeaders
// * @return ResponseInterface // * @return ResponseInterface
// */ // */
// public static function put($url, $dataArray, $httpHeaders = []) // public static function put($url, $data, $httpHeaders = [])
// { // {
// $client = new Client(['timeout' => 30]); // $client = new Client(['timeout' => 30]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $dataArray]); // return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
// } // }
// //
// /** // /**
...@@ -86,34 +87,33 @@ class HttpRequestJson ...@@ -86,34 +87,33 @@ class HttpRequestJson
*/ */
public static function get($url, $headers = []) public static function get($url, $headers = [])
{ {
self::prepareRequest($headers);
return CurlRequest::get($url, $headers); return CurlRequest::get($url, $headers);
} }
/** /**
* post请求 * post请求
* @param $url * @param $url
* @param $dataArray * @param $data
* @param array $headers * @param array $headers
* @return array * @return array
*/ */
public static function post($url, $dataArray, $headers = []) public static function post($url, $data, $headers = [])
{ {
self::prepareRequest($headers, $dataArray); [$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::post($url, $dataArray, $headers); return CurlRequest::post($url, $data, $headers);
} }
/** /**
* put请求 * put请求
* @param $url * @param $url
* @param $dataArray * @param $data
* @param array $headers * @param array $headers
* @return array * @return array
*/ */
public static function put($url, $dataArray, $headers = []) public static function put($url, $data, $headers = [])
{ {
self::prepareRequest($headers, $dataArray); [$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::put($url, $dataArray, $headers); return CurlRequest::put($url, $data, $headers);
} }
/** /**
...@@ -124,7 +124,6 @@ class HttpRequestJson ...@@ -124,7 +124,6 @@ class HttpRequestJson
*/ */
public static function delete($url, $headers = []) public static function delete($url, $headers = [])
{ {
self::prepareRequest($headers);
return CurlRequest::delete($url, $headers); return CurlRequest::delete($url, $headers);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment