0
03
11
47
为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕容3067478 2019-11-26 10:40:04
有人知道post使用JSON 的正确方法Guzzle吗?$request = $this->client->post(self::URL_REGISTER,array(                'content-type' => 'application/json'        ),array(json_encode($_POST)));我internal server error从服务器收到响应。它可以使用Chrome浏览器Postman。

3 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

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


use GuzzleHttp\Client;


$client = new Client();


$response = $client->post('url', [

    GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar']

]);


查看完整回答
反对 回复 2019-11-26
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

对于Guzzle <= 4:


这是原始的发布请求,因此将JSON放入正文即可解决问题


$request = $this->client->post($url,array(

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

        ),array());

$request->setBody($data); #set body!

$response = $request->send();


return $response;


查看完整回答
反对 回复 2019-11-26

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号