Skip to content

Commit 489ba0d

Browse files
committed
POST 请求数据支持对象
1 parent da976f4 commit 489ba0d

2 files changed

Lines changed: 54 additions & 37 deletions

File tree

src/HttpRequest.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class HttpRequest
2626
/**
2727
* 发送内容,可以是字符串、数组(支持键值、Yurun\Util\YurunHttp\Http\Psr7\UploadedFile,其中键值会作为html编码,文件则是上传).
2828
*
29-
* @var mixed
29+
* @var string|object|array
3030
*/
3131
public $content;
3232

@@ -345,7 +345,7 @@ public function url($url)
345345
/**
346346
* 设置发送内容,requestBody的别名.
347347
*
348-
* @param mixed $content 发送内容,可以是字符串、数组
348+
* @param string|object|array $content 发送内容,可以是字符串、数组
349349
*
350350
* @return static
351351
*/
@@ -369,7 +369,7 @@ public function params($params)
369369
/**
370370
* 设置请求主体.
371371
*
372-
* @param string|string|array $requestBody 发送内容,可以是字符串、数组
372+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组
373373
*
374374
* @return static
375375
*/
@@ -860,7 +860,7 @@ public function connectionPool($connectionPool)
860860
/**
861861
* 处理请求主体.
862862
*
863-
* @param string|string|array $requestBody
863+
* @param string|object|array $requestBody
864864
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
865865
*
866866
* @return array
@@ -872,7 +872,7 @@ protected function parseRequestBody($requestBody, $contentType)
872872
{
873873
$body = $requestBody;
874874
}
875-
elseif (\is_array($requestBody))
875+
elseif (\is_array($requestBody) || \is_object($requestBody))
876876
{
877877
switch ($contentType)
878878
{
@@ -906,10 +906,10 @@ protected function parseRequestBody($requestBody, $contentType)
906906
/**
907907
* 构建请求类.
908908
*
909-
* @param string $url 请求地址,如果为null则取url属性值
910-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
911-
* @param string|null $method 请求方法,GET、POST等
912-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
909+
* @param string $url 请求地址,如果为null则取url属性值
910+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
911+
* @param string|null $method 请求方法,GET、POST等
912+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
913913
*
914914
* @return \Yurun\Util\YurunHttp\Http\Request
915915
*/
@@ -963,10 +963,10 @@ public function buildRequest($url = null, $requestBody = null, $method = null, $
963963
/**
964964
* 发送请求,所有请求的老祖宗.
965965
*
966-
* @param string|null $url 请求地址,如果为null则取url属性值
967-
* @param string|array|null $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
968-
* @param string $method 请求方法,GET、POST等
969-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
966+
* @param string|null $url 请求地址,如果为null则取url属性值
967+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
968+
* @param string $method 请求方法,GET、POST等
969+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
970970
*
971971
* @return \Yurun\Util\YurunHttp\Http\Response|null
972972
*/
@@ -980,10 +980,10 @@ public function send($url = null, $requestBody = null, $method = null, $contentT
980980
/**
981981
* 发送 Http2 请求不调用 recv().
982982
*
983-
* @param string|null $url 请求地址,如果为null则取url属性值
984-
* @param string|array|null $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
985-
* @param string $method 请求方法,GET、POST等
986-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
983+
* @param string|null $url 请求地址,如果为null则取url属性值
984+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
985+
* @param string $method 请求方法,GET、POST等
986+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
987987
*
988988
* @return \Yurun\Util\YurunHttp\Http\Response|null
989989
*/
@@ -999,8 +999,8 @@ public function sendHttp2WithoutRecv($url = null, $requestBody = null, $method =
999999
/**
10001000
* GET请求
10011001
*
1002-
* @param string $url 请求地址,如果为null则取url属性值
1003-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1002+
* @param string $url 请求地址,如果为null则取url属性值
1003+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
10041004
*
10051005
* @return \Yurun\Util\YurunHttp\Http\Response|null
10061006
*/
@@ -1025,9 +1025,9 @@ public function get($url = null, $requestBody = null)
10251025
/**
10261026
* POST请求
10271027
*
1028-
* @param string $url 请求地址,如果为null则取url属性值
1029-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1030-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
1028+
* @param string $url 请求地址,如果为null则取url属性值
1029+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1030+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
10311031
*
10321032
* @return \Yurun\Util\YurunHttp\Http\Response|null
10331033
*/
@@ -1039,8 +1039,8 @@ public function post($url = null, $requestBody = null, $contentType = null)
10391039
/**
10401040
* HEAD请求
10411041
*
1042-
* @param string $url 请求地址,如果为null则取url属性值
1043-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1042+
* @param string $url 请求地址,如果为null则取url属性值
1043+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
10441044
*
10451045
* @return \Yurun\Util\YurunHttp\Http\Response|null
10461046
*/
@@ -1052,9 +1052,9 @@ public function head($url = null, $requestBody = null)
10521052
/**
10531053
* PUT请求
10541054
*
1055-
* @param string $url 请求地址,如果为null则取url属性值
1056-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1057-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
1055+
* @param string $url 请求地址,如果为null则取url属性值
1056+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1057+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
10581058
*
10591059
* @return \Yurun\Util\YurunHttp\Http\Response|null
10601060
*/
@@ -1066,9 +1066,9 @@ public function put($url = null, $requestBody = null, $contentType = null)
10661066
/**
10671067
* PATCH请求
10681068
*
1069-
* @param string $url 请求地址,如果为null则取url属性值
1070-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1071-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
1069+
* @param string $url 请求地址,如果为null则取url属性值
1070+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1071+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
10721072
*
10731073
* @return \Yurun\Util\YurunHttp\Http\Response|null
10741074
*/
@@ -1080,9 +1080,9 @@ public function patch($url = null, $requestBody = null, $contentType = null)
10801080
/**
10811081
* DELETE请求
10821082
*
1083-
* @param string $url 请求地址,如果为null则取url属性值
1084-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1085-
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
1083+
* @param string $url 请求地址,如果为null则取url属性值
1084+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1085+
* @param string|null $contentType 内容类型,支持null/json,为null时不处理
10861086
*
10871087
* @return \Yurun\Util\YurunHttp\Http\Response|null
10881088
*/
@@ -1094,10 +1094,10 @@ public function delete($url = null, $requestBody = null, $contentType = null)
10941094
/**
10951095
* 直接下载文件.
10961096
*
1097-
* @param string $fileName 保存路径,如果以 .* 结尾,则根据 Content-Type 自动决定扩展名
1098-
* @param string $url 下载文件地址
1099-
* @param string|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1100-
* @param string $method 请求方法,GET、POST等,一般用GET
1097+
* @param string $fileName 保存路径,如果以 .* 结尾,则根据 Content-Type 自动决定扩展名
1098+
* @param string $url 下载文件地址
1099+
* @param string|object|array $requestBody 发送内容,可以是字符串、数组,如果为空则取content属性值
1100+
* @param string $method 请求方法,GET、POST等,一般用GET
11011101
*
11021102
* @return \Yurun\Util\YurunHttp\Http\Response|null
11031103
*/

tests/unit/HttpRequestTest/HttpRequestTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ public function testPostParams()
102102
$data = $response->json(true);
103103
$this->assertEquals('POST', isset($data['method']) ? $data['method'] : null);
104104
$this->assertEquals($time, isset($data['post']['time']) ? $data['post']['time'] : null);
105+
106+
$http = new HttpRequest();
107+
$time = time();
108+
$params = new \stdClass();
109+
$params->time = $time;
110+
$this->assertResponse($response);
111+
$data = $response->json(true);
112+
$this->assertEquals('POST', isset($data['method']) ? $data['method'] : null);
113+
$this->assertEquals($time, isset($data['post']['time']) ? $data['post']['time'] : null);
105114
});
106115
}
107116

@@ -410,6 +419,14 @@ public function testBody()
410419
$response = $http->post($this->host . '?a=body', $data, 'json');
411420
$this->assertResponse($response);
412421
$this->assertEquals(json_encode($data), $response->body());
422+
423+
$http = new HttpRequest();
424+
$data = new \stdClass();
425+
$data->id = 1;
426+
$data->name = 'YurunHttp';
427+
$response = $http->post($this->host . '?a=body', $data, 'json');
428+
$this->assertResponse($response);
429+
$this->assertEquals(json_encode($data), $response->body());
413430
});
414431
}
415432

0 commit comments

Comments
 (0)