Commit 9f9dd18e authored by 王源's avatar 王源 🎧

优化

parent e0eac877
......@@ -19,13 +19,14 @@ namespace Meibuyu\Micro\Tools;
class HttpRequestJson
{
protected static function prepareRequest(&$headers = [], &$data = [])
protected static function prepareRequest($headers, $data = [])
{
$data = json_encode($data);
if (!$data) {
if (!empty($data)) {
$headers['Content-type'] = 'application/json';
$headers['Content-Length'] = strlen($data);
}
return [$headers, $data];
}
// /**
......@@ -43,27 +44,27 @@ class HttpRequestJson
// /**
// * post请求
// * @param $url
// * @param $dataArray
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function post($url, $dataArray, $httpHeaders = [])
// public static function post($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $dataArray]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * put请求
// * @param $url
// * @param $dataArray
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function put($url, $dataArray, $httpHeaders = [])
// public static function put($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $dataArray]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
......@@ -86,21 +87,20 @@ class HttpRequestJson
*/
public static function get($url, $headers = [])
{
self::prepareRequest($headers);
return CurlRequest::get($url, $headers);
}
/**
* post请求
* @param $url
* @param $dataArray
* @param $data
* @param array $headers
* @return array
*/
public static function post($url, $dataArray, $headers = [])
public static function post($url, $data, $headers = [])
{
self::prepareRequest($headers, $dataArray);
return CurlRequest::post($url, $dataArray, $headers);
[$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::post($url, $data, $headers);
}
/**
......@@ -112,8 +112,8 @@ class HttpRequestJson
*/
public static function put($url, $data, $headers = [])
{
self::prepareRequest($headers, $dataArray);
return CurlRequest::put($url, $dataArray, $headers);
[$headers, $data] = self::prepareRequest($headers, $data);
return CurlRequest::put($url, $data, $headers);
}
/**
......@@ -124,7 +124,6 @@ class HttpRequestJson
*/
public static function delete($url, $headers = [])
{
self::prepareRequest($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