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

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


$request = $this->client->post(self::URL_REGISTER,array(

                'content-type' => 'application/json'

        ),array(json_encode($_POST)));

我internal server error从服务器收到响应。它可以使用Chrome浏览器Postman。


慕容3067478
浏览 5492回答 3
3回答

沧海一幻觉

对于Guzzle 5和6,您可以这样做:use GuzzleHttp\Client;$client = new Client();$response = $client->post('url', [    GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar']]);

开心每一天1111

对于Guzzle <= 4:这是原始的发布请求,因此将JSON放入正文即可解决问题$request = $this->client->post($url,array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'content-type' => 'application/json'&nbsp; &nbsp; &nbsp; &nbsp; ),array());$request->setBody($data); #set body!$response = $request->send();return $response;
打开App,查看更多内容
随时随地看视频慕课网APP