Php 如何在Guzzle 5中发送PUT请求的参数?
我有此代码用于发送POST请求的参数,它可以工作:Php 如何在Guzzle 5中发送PUT请求的参数?,php,rest,guzzle,Php,Rest,Guzzle,我有此代码用于发送POST请求的参数,它可以工作: $client = new GuzzleHttp\Client(); $request = $client->createRequest('POST', 'http://example.com/test.php'); $body = $request->getBody(); $request->getBody()->replaceFields([ 'name' => 'Bob' ]); 但是,当我将PO
$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中执行此操作?根据
主体选项用于控制封闭实体的主体
请求(例如,PUT、POST、补丁)
put
的文件化方法是:
$client = new GuzzleHttp\Client();
$client->put('http://httpbin.org', [
'headers' => ['X-Foo' => 'Bar'],
'body' => [
'field' => 'abc',
'other_field' => '123'
],
'allow_redirects' => false,
'timeout' => 5
]);
编辑
根据您的评论:
您缺少
createRequest
函数的第三个参数-组成post
或put
数据的键/值对数组:
$request = $client->createRequest('PUT', '/put', ['body' => ['foo' => 'bar']]);
当服务等待json原始数据时
$request = $client->createRequest('PUT', '/yourpath', ['json' => ['key' => 'value']]);
或
如果您使用的是版本6,则可以通过以下方式发出请求:
$client = new \GuzzleHttp\Client();
$response = $client->put('http://example.com/book/1', [
'query' => [
'price' => '50',
]
]);
print_r($response->getBody()->getContents());
在Guzzle 6中,如果您希望将JSON数据传递给PUT请求,则可以按如下方式实现:
$aObj = ['name' => 'sdfsd', 'language' => 'En'];
$headers = [
"User-Agent" => AGENT,
"Expect" => "100-continue",
"api-origin" => "LTc",
"Connection" => "Keep-Alive",
"accept" => "application/json",
"Host" => "xyz.com",
"Accept-Encoding"=> " gzip, deflate",
"Cache-Control"=> "no-cache",
"verify" => false,
"Content-Type" => "application/json"
];
$client = new GuzzleHttp\Client([
'auth' => ['testUsername', 'testPassword'],
'timeout' => '10000',
'base_uri' => YOUR_API_URL,
'headers' => $headers
]);
$oResponse = $client->request('PUT', '/user/UpdateUser?format=json', ['body' => json_encode( $aObj, JSON_UNESCAPED_SLASHES)]);
$oUser = json_decode( $oResponse->getBody());
问题是我希望能够使用createRequest()方法,因为我正在确定以编程方式使用哪种方法。您缺少
createRequest
函数的第三个参数-一个组成post
或put
数据的键/值对数组:$request=$client->createRequest('put','put',['json'=>['foo'=>'bar']];
答案中的链接不再有效,但这是Guzzle的最新版本:或者更准确地说。body
已被弃用,应该是Guzzle 6中的form params
$aObj = ['name' => 'sdfsd', 'language' => 'En'];
$headers = [
"User-Agent" => AGENT,
"Expect" => "100-continue",
"api-origin" => "LTc",
"Connection" => "Keep-Alive",
"accept" => "application/json",
"Host" => "xyz.com",
"Accept-Encoding"=> " gzip, deflate",
"Cache-Control"=> "no-cache",
"verify" => false,
"Content-Type" => "application/json"
];
$client = new GuzzleHttp\Client([
'auth' => ['testUsername', 'testPassword'],
'timeout' => '10000',
'base_uri' => YOUR_API_URL,
'headers' => $headers
]);
$oResponse = $client->request('PUT', '/user/UpdateUser?format=json', ['body' => json_encode( $aObj, JSON_UNESCAPED_SLASHES)]);
$oUser = json_decode( $oResponse->getBody());