使用Guzzle使用JSON发送POST请求

$client = new Client();
$url = 'api-url';

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

return $request;

然后我返回了502 Bad Gateway 以及Resource解释为Document,但以MIME类型application / json传输

我需要用一些json发出POST请求。 如何在Laravel中使用Guzzle做到这一点?

Give it a try

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

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

Take a look..

$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();