如何在Guzzle 5中发送PUT请求的参数? [英] How do I send parameters for a PUT request in Guzzle 5?

查看:638
本文介绍了如何在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?

推荐答案

根据

body 选项用于控制封闭的实体的主体 请求(例如PUT,POST,PATCH).

The body option is used to control the body of an entity enclosing request (e.g., PUT, POST, PATCH).

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函数的第三个参数-组成postput数据的键/值对数组:

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请求的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆