本文介绍了如何在Guzzle 5中发送PUT请求的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有用于发送POST请求参数的代码,该代码有效:
I have this code for sending parameters for a POST request, which works:
$client = new GuzzleHttp\Client();
$request = $client->createRequest('POST', 'http://example.com/test.php');
$body = $request->getBody();
$request->getBody()->replaceFields([
'name' => 'Bob'
]);
但是,当我将POST更改为PUT时,会出现此错误:
However, when I change POST to PUT, I get this error:
Call to a member function replaceFields() on a non-object
这是因为getBody返回null.
This is because getBody is returning null.
在体内发送PUT参数实际上正确吗?还是应该在URL中完成?
Is it actually correct to send PUT parameters in the body? Or should I do it in the URL?
推荐答案
根据手册,
put
ing的记录方法为:
The documented method of 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
Based on your comment:
您缺少createRequest
函数的第三个参数-组成post
或put
数据的键/值对数组:
You are missing the third parameter of the createRequest
function - an array of key/value pairs making up the post
or put
data:
$request = $client->createRequest('PUT', '/put', ['body' => ['foo' => 'bar']]);
这篇关于如何在Guzzle 5中发送PUT请求的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!