我在这里找到了以下 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);
?>