使用Guzzle使用JSON发送POST请求

问题描述 投票:1回答:2
$client = new Client();
$url = 'api-url';

$request = $client->post($url, [
    'headers' => ['Content-Type' => 'application/json'],
    'json' => ['token' => 'foo']
]);

return $request;

我收回502 Bad Gateway和资源解释为文档,但转移MIME类型application / json

我需要用一些json发出POST请求。我怎么能在Laravel的Guzzle中做到这一点?

php laravel guzzle
2个回答
5
投票

试试看

$response = $client->post('http://api.example.com', [
    'json' => [
       'key' => 'value'
     ]
]);

dd($response->getBody()->getContents());

0
投票

看一看..

$client = new Client();

$url = 'api-url';

$headers = array('Content-Type: application/json');

$data = array('json' => array('token' => 'foo'));

$request = new Request("POST", $url, $headers, json_encode($data));

$response = $client->send($request, ['timeout' => 10]);

$data = $response->getBody()->getContents();
© www.soinside.com 2019 - 2024. All rights reserved.