PHP script CURL string returns no result for long string (612300 chars) works if string below 500k characters

huangapple go评论60阅读模式
英文:

PHP script CURL string returns no result for long string (612300 chars) works if string below 500k characters

问题

我希望有人能帮我调试这个问题。我有一个 PHP 脚本,它可以正常地获取一个加密数据的字符串,但当返回的字符串长度超过约 500,000 个字符时,脚本就无法正常工作了。

目前,我的脚本输出的字符串长度为 612,300 个字符,此时 CURL 返回一个空结果。

然而,如果我将字符串长度降低到 500,000 个字符以下,CURL 就可以显示结果中的数据。

有人知道为什么会这样吗?我该如何处理才能允许无限长度的返回字符串?

这个问题与 mod_security 无关,因为它没有触发任何问题,似乎只与字符串长度有关。

谢谢。

英文:

I'm hoping someone can help me debug this issue, I have a PHP script that works fine gathering a single string of encrypted data, up until the string returned is above around 500,000 characters.

My script currently is outputting at 612,300 characters, at this point the CURL returns an empty result.

If however I lower the string to below 500,000 characters, CURL can display the data in the result.

Does anyone know why this is, and how I could go about allowing an unlimited return string length?

The issue isn't with mod_security as it's not triggering any issues there, it seems to be solely related to the string length.

Thanks

答案1

得分: 2

在进行了一番深入搜索后,我自己解决了这个问题。问题出在设置 CURLOPT_BUFFERSIZE 上,默认情况下它的限制是512k。

将其增加到字符串中的字节数/字符数,问题就得到了解决。同时,还可以通过调整超时设置来解决问题,具体请参考下面的代码:

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 1000000);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 30); 
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
英文:

Solved this myself after some heavy searching, the issue was the setting CURLOPT_BUFFERSIZE which limits itself to 512k by default.

Increasing it to the amount of bytes / characters in the string fixed this, as did extending the timeout settings, see below:

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 1000000);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 30); 
curl_setopt($ch, CURLOPT_TIMEOUT, 300);

答案2

得分: 0

看起来你遇到了一个问题,即PHP中的cURL无法处理特定大小的响应。可能有几个因素导致这个问题,包括内存限制、超时设置或其他服务器端限制。

以下是一些调试和可能解决该问题的步骤:

1. 增加PHP内存限制

如果脚本内存不足,你可以在脚本顶部添加以下行来增加内存限制:

ini_set('memory_limit', '512M'); // 或者根据需要增加更高的值

2. 增加cURL超时时间

你可能还需要增加cURL的超时设置,因为获取大量数据可能需要更长时间。你可以使用以下代码来调整超时时间:

curl_setopt($curl, CURLOPT_TIMEOUT, 300); // 5分钟

3. 检查错误

确保在执行请求后检查cURL错误。这可能会提供更多关于出错原因的信息:

$err = curl_error($curl);
if ($err) {
  echo "cURL错误:" . $err;
}

4. 增加POST大小限制(如果适用)

如果你通过POST发送大量数据,可能需要在PHP配置文件(`php.ini`)中增加POST大小限制:

post_max_size = 64M

5. 检查服务器配置

如果以上步骤都不起作用,可能值得与你的托管提供商联系或检查服务器配置。Apache、Nginx或其他Web服务器可能有需要调整的限制。

6. 直接调试cURL

你可以使用命令行cURL直接测试端点(在PHP之外)。这可能有助于缩小问题范围,确定问题是与PHP还是其他因素有关:

curl -X GET "your_url_here"

7. 考虑分块响应

如果一次处理如此大的响应不可行,你可以考虑更改服务器发送响应的方式。你可以将其分成块,并在客户端逐个处理。

如果对服务器配置文件进行任何更改,请记得重新启动Web服务器(如Apache、Nginx等)。

英文:

It looks like you're encountering an issue where cURL in PHP is not able to handle a response of a certain size. There could be several factors contributing to this, including memory limitations, timeout settings, or some other server-side constraint.

Here are some steps to debug and potentially resolve the issue:

<h3>1. Increase PHP Memory Limit</h3>
If the script is running out of memory, you can increase the memory limit by adding the following line at the top of your script:<br><br>

ini_set(&#39;memory_limit&#39;, &#39;512M&#39;); // or even higher if necessary

<h3>2. Increase cURL Timeout:</h3>

You might also want to increase the cURL timeout settings, as fetching a large amount of data may take longer. You can adjust the timeout using:

curl_setopt($curl, CURLOPT_TIMEOUT, 300); // 5 minutes

<h3>3. Check for Errors</h3>
Ensure that you are checking for cURL errors after executing the request. This might give you more information about what's going wrong:<br><br>

$err = curl_error($curl);
if ($err) {
  echo &quot;cURL Error: &quot; . $err;
}

<h3>4. Increase POST Size (if applicable)</h3>
If you are sending a large amount of data via POST, you may need to increase the POST size limits in your PHP configuration (php.ini):<br><br>

post_max_size = 64M

<h3>5. Check Server Configuration</h3>
If none of the above steps work, it might be worth checking with your hosting provider or inspecting the server configuration. Apache, Nginx, or other web servers may have limits that need to be adjusted.
<br><br>
<h3>6. Debugging cURL Directly</h3>
You can use command-line cURL to test the endpoint directly (outside of PHP). This might help you narrow down whether the issue is with PHP or something else:<br><br>

curl -X GET &quot;your_url_here&quot;

<h3>7. Consider Chunking the Response</h3>
If it's not feasible to handle such a large response in one go, you might consider changing the way your server sends the response. You could split it into chunks and handle it piece by piece on the client side.

Remember to restart your web server (Apache, Nginx, etc.) if you make any changes to the server configuration files.

huangapple
  • 本文由 发表于 2023年8月9日 12:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定