如何使用Guzzle以JSON格式发送POST请求?

use*_*466 160 php guzzle postman

有没有人知道post使用JSON 的正确方法Guzzle

$request = $this->client->post(self::URL_REGISTER,array(
                'content-type' => 'application/json'
        ),array(json_encode($_POST)));
Run Code Online (Sandbox Code Playgroud)

我收到internal server error服务器的回复.它适用于Chrome Postman.

Mic*_*vic 229

对于Guzzle 5和6,您可以这样做:

use GuzzleHttp\Client;

$client = new Client();

$response = $client->post('url', [
    GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar']
]);
Run Code Online (Sandbox Code Playgroud)

文件

  • 这是正确的做法([这里的官方例子](http://docs.guzzlephp.org/en/latest/request-options.html#json)) (11认同)
  • @MichalGallovic这是一样的.使用常量的目的是避免拼写错误.使用不存在的常量会引发错误,但发送无用选项(例如`jsson`)不会引发任何错误,您可能需要一些时间来查找错字. (7认同)
  • 建议对选项数组键使用`RequestOptions`常量(在这种情况下为`GuzzleHttp\RequestOptions :: JSON`) - 它使拼写错误更易于检测,因为它们突然变成通知而不是只是等待引起麻烦的静默错误. (4认同)

use*_*466 41

对于Guzzle <= 4:

这是一个原始的帖子请求,因此将JSON放入正文解决了问题

$request = $this->client->post($url,array(
                'content-type' => 'application/json'
        ),array());
$request->setBody($data); #set body!
$response = $request->send();

return $response;
Run Code Online (Sandbox Code Playgroud)

  • 这不再适用于GuzzleHttp.@Charlie有正确的答案 (7认同)
  • 如果你想在 Guzzle 6 中设置内容类型标题,你可以这样做:`$client-&gt;post($url, ['body' =&gt; $string, 'headers' =&gt; ['Content-type' =&gt; '应用程序/json']]);` (3认同)

Fra*_*oth 41

简单而基本的方式(guzzle6):

$client = new Client([
    'headers' => [ 'Content-Type' => 'application/json' ]
]);

$response = $client->post('http://api.com/CheckItOutNow',
    ['body' => json_encode(
        [
            'hello' => 'World'
        ]
    )]
);
Run Code Online (Sandbox Code Playgroud)

要获取响应状态代码和正文的内容,我执行了以下操作:

echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
echo '<pre>' . var_export($response->getBody()->getContents(), true) . '</pre>';
Run Code Online (Sandbox Code Playgroud)

  • 这真的是一种简单易行的方法.解决了我设置正文和标题的问题.非常感谢 (2认同)

dav*_*ash 27

这对我有用(使用Guzzle 6)

$client = new Client(); 
$result = $client->post('http://api.example.com', [
            'json' => [
                'value_1' => 'number1',
                'Value_group' =>  
                             array("value_2" => "number2",
                                    "value_3" => "number3")
                    ]
                ]);

echo($result->getBody()->getContents());
Run Code Online (Sandbox Code Playgroud)


Cha*_*ade 25

$client = new \GuzzleHttp\Client();

$body['grant_type'] = "client_credentials";
$body['client_id'] = $this->client_id;
$body['client_secret'] = $this->client_secret;

$res = $client->post($url, [ 'body' => json_encode($body) ]);

$code = $res->getStatusCode();
$result = $res->json();
Run Code Online (Sandbox Code Playgroud)

  • 这是否也设置了正确的标题?我认为 `['json' =&gt; $body]` 是这里更好的方法,正如迈克尔的回答所提到的。 (2认同)

Nur*_*uda 16

您可以使用硬编码json属性作为键,也可以方便地使用GuzzleHttp\RequestOptions::JSON常量。

这是使用硬编码json字符串的示例。

use GuzzleHttp\Client;

$client = new Client();

$response = $client->post('url', [
    'json' => ['foo' => 'bar']
]);
Run Code Online (Sandbox Code Playgroud)

请参阅文档


小智 7

Guzzle 6.2对我有用:

$gClient =  new \GuzzleHttp\Client(['base_uri' => 'www.foo.bar']);
$res = $gClient->post('ws/endpoint',
                            array(
                                'headers'=>array('Content-Type'=>'application/json'),
                                'json'=>array('someData'=>'xxxxx','moreData'=>'zzzzzzz')
                                )
                    );
Run Code Online (Sandbox Code Playgroud)

根据文档guzzle做json_encode


Yam*_*raf 7

$client = new \GuzzleHttp\Client(['base_uri' => 'http://example.com/api']);

$response = $client->post('/save', [
    'json' => [
        'name' => 'John Doe'
    ]
]);

return $response->getBody();
Run Code Online (Sandbox Code Playgroud)


Sta*_*kin 7

$client->request('POST',... 的解决方案

对于那些正在使用的人来说,$client->request这是创建 JSON 请求的方法:

$client = new Client();
$res = $client->request('POST', "https://some-url.com/api", [
    'json' => [
        'paramaterName' => "parameterValue",
        'paramaterName2' => "parameterValue2",
    ]
    'headers' => [
    'Content-Type' => 'application/json',
    ]
]);
Run Code Online (Sandbox Code Playgroud)

Guzzle JSON 请求参考