我有这个代码用于发送POST请求的参数,它有效:
$client = new GuzzleHttp\Client();
$request = $client->createRequest('POST', 'http://example.com/test.php');
$body = $request->getBody();
$request->getBody()->replaceFields([
'name' => 'Bob'
]);
Run Code Online (Sandbox Code Playgroud)
但是,当我将POST更改为PUT时,我收到此错误:
Call to a member function replaceFields() on a non-object
Run Code Online (Sandbox Code Playgroud)
这是因为getBody返回null.
在体内发送PUT参数实际上是否正确?或者我应该在URL中执行此操作?
Ric*_*ing 14
根据手册,
所述体选项用于控制一个实体内附请求(例如,PUT,POST,贴片)的主体上.
记录的put'ing 方法是:
$client = new GuzzleHttp\Client();
$client->put('http://httpbin.org', [
'headers' => ['X-Foo' => 'Bar'],
'body' => [
'field' => 'abc',
'other_field' => '123'
],
'allow_redirects' => false,
'timeout' => 5
]);
Run Code Online (Sandbox Code Playgroud)
根据您的评论:
您缺少createRequest函数的第三个参数- 构成post或put数据的键/值对数组:
$request = $client->createRequest('PUT', '/put', ['body' => ['foo' => 'bar']]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14035 次 |
| 最近记录: |