我有这段代码用于发送 POST 请求的参数,它的工作原理是:
$client = new GuzzleHttp\Client();
$request = $client->createRequest('POST', 'http://example.com/test.php');
$body = $request->getBody();
$request->getBody()->replaceFields([
'name' => 'Bob'
]);
但是,当我将 POST 更改为 PUT 时,出现以下错误:
Call to a member function replaceFields() on a non-object
这是因为 getBody 返回 null。
在正文中发送 PUT 参数实际上是否正确?或者我应该在 URL 中执行此操作?
根据手册 https://web.archive.org/web/20150116073119/http://guzzle.readthedocs.org/en/latest/clients.html,
The body选项用于控制包含实体的主体
请求(例如,PUT、POST、PATCH)。
记录的方法put
'ing is:
$client = new GuzzleHttp\Client();
$client->put('http://httpbin.org', [
'headers' => ['X-Foo' => 'Bar'],
'body' => [
'field' => 'abc',
'other_field' => '123'
],
'allow_redirects' => false,
'timeout' => 5
]);
Edit
根据您的评论:
您缺少第三个参数createRequest
function - 组成的键/值对的数组post
or put
data:
$request = $client->createRequest('PUT', '/put', ['body' => ['foo' => 'bar']]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)