问题描述
使用Guzzle(版本3),我想以原始"模式指定POST请求的正文.我目前正在尝试:
With Guzzle (version 3), I'd like to specify the body of a POST request in "raw" mode. I'm currently trying this:
$guzzleRequest = $client->createRequest(
'POST',
$uri,
null,
'un=one&deux=two'
);
但是这是行不通的.如果转储$guzzleRequest
,我可以看到postFields->data
为空.之后再使用$guzzleRequest->setBody()
并没有帮助.
But it kind of doesn't work. If I dump my $guzzleRequest
I can see that postFields->data
is empty. Using $guzzleRequest->setBody()
afterwards doesn't help.
但是,如果我将主体指定为['un'=>'one', 'deux'=>'two']
,它将按预期工作.
However if I specify the body as ['un'=>'one', 'deux'=>'two']
, it works as expected.
如何将请求的正文指定为'un=one&deux=two'
?
How can I specify the body of the request as 'un=one&deux=two'
?
推荐答案
首先,我强烈建议您升级到Guzzle 6,因为不建议使用Guzzle 3并停产.
First I would highly recommend that you upgrade to Guzzle 6 as Guzzle 3 is deprecated and EOL.
自从我使用Guzzle 3已有很长时间了,但我确实相信您需要以下条件:
It has been a long time since I used Guzzle 3 but I do believe you want the following:
$request = $client->post(
$uri,
$header = [],
$params = [
'un' => 'one',
'deux' => 'two',
]);
$response = $request->send();
Guzzle将自动设置Content-Type标头.
Guzzle will automatically set the Content-Type header.
有关更多信息,请参见发布请求文档.
More information is available with the Post Request Documentation.
针对您的评论:
$request = $client->post(
$uri,
$headers = ['Content-Type' => 'application/x-www-form-urlencoded'],
EntityBody::fromString($urlencodedstring)
)
为此,请参考: EntityBody来源和 RequestFactory :: create()
这篇关于用Guzzle指定POST请求的原始正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!