2

我在这里找到了以下 php 代码:

function download_xml()
{
    $url = 'http://tv.sygko.net/tv.xml';

    $ch = curl_init($url);
    $timeout = 5;

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    $data = curl_exec($ch);

    echo("curl_exec was succesful"); //This never gets called

    curl_close($ch);
    return $data;
}

$my_file = 'tvdata.xml';
$handle = fopen($my_file, 'w');
$data = download_xml();
fwrite($handle, $data);

我要做的是在指定的 url 下载 xml 并将其保存到磁盘。echo但是,它会在大约 80% 完成后停止,并且在通话后永远不会到达curl_exec通话。我不知道为什么,但我相信这是因为它的内存不足。因此,我想问是否可以让 curl 每次下载 4kb 时将数据写入文件。如果这是不可能的,有没有人知道一种方法来获取存储在 url 中的 xml 文件,使用 php 下载并存储在我的磁盘上?

非常感谢你,本。

编辑:这是现在的代码,它不起作用。它将数据写入文件,但仍然只有大约 80% 的文档。也许不是因为它超出了内存,而是其他原因?我真的不敢相信将文件从 URL 复制到光盘如此困难......

    <?

$url = 'http://tv.sygko.net/tv.xml';
$my_file = fopen('tvdata.xml', 'w');

$ch = curl_init($url);
$timeout = 300;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $my_file);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096);

curl_exec($ch) OR die("Error in curl_exec()");

echo("got to after curl exec");

fclose($my_file);
curl_close($ch);

    ?>
4

4 回答 4

5

这是一个完整的工作示例:

public function saveFile($url, $dest) {

        if (!file_exists($dest))
                touch($dest);

        $file = fopen($dest, 'w');
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progressCallback');
        curl_setopt($ch, CURLOPT_BUFFERSIZE, (1024*1024*512));
        curl_setopt($ch, CURLOPT_NOPROGRESS, FALSE);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        curl_setopt($ch, CURLOPT_FILE, $file);

        curl_exec($ch);
        curl_close($ch);

        fclose($file);
}
?>

秘密在于将 CURLOPT_NOPROGRESS 设置为 FALSE,然后,CURLOPT_BUFFERSIZE 将为每个到达的 CURLOPT_BUFFERSIZE 字节生成回调报告。值越小,报告的频率越高。这也取决于您的下载速度等,因此不要指望它每 X 秒报告一次,因为它会报告每 X 个接收/传输的字节。

于 2013-03-26T19:52:15.637 回答
3

您的超时设置为 5 秒,这可能太短,具体取决于文档的文件大小。尝试将其增加到 10-15,以确保它有足够的时间来完成传输。

于 2009-10-05T17:17:23.797 回答
2

有一个名为 CURELOPT_FILE 的选项允许您指定curl 应该写入的文件处理程序。我很确定它会做“正确”的事情并在读取时“写入”,避免你的记忆问题

$file = fopen('test.txt', 'w'); //<--------- file handler
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://example.com');
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FILE, $file);   //<------- this is your magic line
curl_exec($ch); 
curl_close($ch);
fclose($file);
于 2009-10-05T17:17:33.597 回答
1

curl_setopt CURLOPT_FILE - 传输应该写入的文件。默认为 STDOUT(浏览器窗口)

http://us2.php.net/manual/en/function.curl-setopt.php

于 2009-10-05T17:07:17.407 回答