PHP Curl json POST不适合我

时间:2015-06-10 22:02:41

标签: php json laravel post curl

您好我尝试使用PHP和Curl发送POST请求。我想发送一个与此类似的JSON

curl_init("domain.com");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, '"json":'.$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);

我已尝试过此代码来完成此操作

{{1}}

我收到请求的laravel项目没有得到任何信息。

编辑:为了进一步解释,我的laravel项目得到了请求,但没有附加任何帖子数据,没有。

2 个答案:

答案 0 :(得分:0)

我不确定你为什么在curl中使用CUSTOMREQUEST选项。 请使用http://curl.haxx.se/libcurl/c/CURLOPT_POST.html

同样在你的laravel中,你可以通过拨打http://php.net/manual/en/function.http-get-request-body.php

来检查身体

答案 1 :(得分:0)

假设$ data_string:

$data_string = '"json":{
  "testString":"test",
  "testInt":1
}';

替换:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, '"json":'.$data_string);

使用:

curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);