php curl post a large json object


php curl post a large json object

所以,我有一个php脚本,它发送一个大的JSON对象,这是我用来这样做的代码。

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);

现在,当数据较小时,请求可以顺利通过。但是,当 json 对象变得很大时,它突然无法发送数据,而是在请求标头中显示"Content-Length: 0",就好像它不包含该对象一样。

我假设数据的大小是有限制的。有没有办法绕过它?

我已经搜索了类似的问题,并找到了发送文件的解决方案。但是我在这里拥有的对象是来自数据库的结果集,因此我更愿意将文件方法作为最后的手段。

谢谢。

所以基本上你已经有了HTTPHEADER,但你错过了内容长度,可以这样尝试:

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-length:' . strlen($data)));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$json_response = curl_exec($curl);