我需要从一个非常大的、通过http访问的远程XML文件中检索少量数据。开始时我只需要文件的一部分,但我访问的文件通常非常大,下载所有文件都会导致超时。似乎可以使用fsockopen在关闭连接之前只拉取所需的量,但我尝试过的都不起作用。
下面是我一直在尝试的一个简化版本。有没有人能告诉我我需要做些什么呢?
<?php
$k = 0;
function socketopen($funcsite, $funcheader){
$fp = fsockopen ($funcsite, 80, $errno, $errstr, 5);
$buffer = NULL;
if ($fp) {
fwrite($fp, "GET " . $funcheader . " HTTP/1.0\r\nHost: " . $funcsite. "\r\n\r\n");
while (!feof($fp)) {
$buffer = fgets($fp, 4096);
echo $buffer;
if($k == 200){
break;
}
$k++;
}
fclose ($fp);
} else {
print "No Response:";
}
return ( html_entity_decode($buffer));
}
$site = "www.remotesite.com";
$header = "/bigdatafile.xml";
$data = socketopen($site, $header);
?>这可以很好地工作,但始终打开和下载整个远程文件。(我实际上使用了与if($k = x)不同的条件,但这无关紧要)。
非常感谢您的帮助。-Jim
发布于 2009-12-28 00:23:55
是否有理由不使用 file_get_contents() ?
$buffer = html_entity_decode(file_get_contents('http://www.remotesite.com/bigdatafile.xml', 0, null, $offsetBytes, $maxlenBytes));对于$offsetBytes $maxlenBytes**.**,只需指定
试试这个:
set_time_limit(0);
echo $buffer = html_entity_decode(file_get_contents('http://www.remotesite.com/bigdatafile.xml', 0, null, 1024, 4096));发布于 2009-12-28 00:04:52
使用此代码,您可以下载整个rss
if (!$xml = simplexml_load_file("http://remotesite.com/bigrss.rss))
{
throw new RuntimeException('Unable to load or parse feed');
}
else
{
file_put_contents($xml,'mybigrss.rss');
}但是,如果您只想获得一些部分,那么请执行以下操作;
$limit = 512000; // set here a limit
$sourceData = fread($s_handle,$limit);
// your code ect..或者使用eof
$source='';
while (!feof($s_handle))
$source.=fread($s_handle,1024); // set limit https://stackoverflow.com/questions/1966217
复制相似问题