doucuo4413 2012-01-15 00:45
浏览 311

PHP:curl写入内存而不是提供文件流

about this basic curlscipt (php):

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urltofile);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 

curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_exec ($ch);
curl_close ($ch);

php.ini max memory=64MB

this script works withou problems om server1 (php 5.2.17 $urltofile, even when $urltofile has a size of eg 2GB. But on server2 also 'php v5.2.17) I get this error when running the script: (error_log)

[DATE] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate xxxxxxx bytes) in /home/user/public_html/test.php on line x

It seems that on server 2, the file is fully written to the memory before is get outputted, while server1 streams the file, like it should be.

  • 写回答

1条回答 默认 最新

  • dqba94619 2012-02-01 03:29
    关注

    Well is max_memory_size the same on both servers? Also, are the other ini settings the same across servers?

    Regardless of these things, you may be able to force CURL to maintain a smaller file than PHP's max_memory_size by setting CURLOPT_BUFFERSIZE, but even then I doubt that's the problem... If you're reading the remote file into a variable then no matter what the buffer size, PHP will eat up memory trying to store the entire remote file there. So it's not the buffer that's exhausting the memory, it's the contents of the remote file.

    My bet is if you specify CURLOPT_FILE, CURL will write the contents of the remote file to disk in a buffered fashion and you will be able to pull down the file on both servers without changing max_memory_size.

    EDIT:

    I now see where output_buffering would come into play with curl, and sure enough this is the case if CURLOPT_RETURNTRANSFER isn't set to true. This would be something to check if OP is using _ob_start()_ et al. somewhere; but that could be causing the problem if it is indeed enabled. Most likely this is the case on server2 as @DaveRandom suggested.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测