简体   繁体   English

PHP curl发布一个大的json对象

[英]php curl post a large json object

So, I have a php script which sends a large JSON object and this is the code I am using to do so. 所以,我有一个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);

Now when the data is small, the request goes through smoothly. 现在,当数据较小时,请求可以顺利进行。 However, when the json object becomes huge it suddenly fails to send the data and instead is shows "Content-Length: 0" in the request header as if it did not include the object. 但是,当json对象变得巨大时,它突然无法发送数据,而是在请求标头中显示“ Content-Length:0”,就好像它不包含该对象一样。

I am assuming there is a limit to how large the data could be. 我假设数据的大小是有限制的。 Is there a way to bypass that? 有办法绕过吗?

I have searched for similar issues and I found a solution for sending files. 我已经搜索了类似的问题,并且找到了发送文件的解决方案。 But the object i have here is a result set from the database and therefore I would prefer to keep the file approach as a last resort. 但是我在这里拥有的对象是数据库的结果集,因此我宁愿将文件方法作为最后的选择。

Thanks. 谢谢。

So basically you have the HTTPHEADER already but you're missing the Content-length one try it like so: 因此,基本上,您已经有了HTTPHEADER,但是您缺少Content-length,请尝试如下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM