2
 header('Access-Control-Allow-Origin: *');
 $tmpFile = 'tmpFile.txt';

 $val="http://rss.news.yahoo.com/rss/topstories";
  $curlHandle = curl_init($val);  
 $filePointer = fopen($tmpFile, "w");  
 curl_setopt($curlHandle, CURLOPT_FILE, $filePointer);  
 curl_exec($curlHandle);
 curl_close($curlHandle);
 fclose($filePointer); 

 $linesArr = file($tmpFile);  
 foreach($linesArr as $eachLine){
 echo($eachLine);
}

该程序应该从 yahoo rss 站点获取所有材料并将它们输出到 tmpFile。

执行程序后,我打开了 tmpFile.txt。表明

c1.ops.sp1.yahoo.com 未压缩/分块 Wed Apr 11 01:46:41 UTC 2012 -->

这看起来不对。我粘贴了网址http://rss.news.yahoo.com/rss/topstories有很多材料返回。

4

2 回答 2

1

你需要的是curl_setopt($curlHandle, CURLOPT_ENCODING , "gzip");......它的饲料yahoo用途......compressionrss

附加信息包括。

A. CURLOPT_USERAGENT.... 如果您不这样做,那就太好了;不要开始看起来像垃圾邮件

B. CURLOPT_TIMEOUT...只是为了效率

C. CURLOPT_FOLLOWLOCATION.. 因为干净的 URL 和路由的问题

工作代码

header ( 'Access-Control-Allow-Origin: *' );
$tmpFile = 'out.txt';

$val = "http://rss.news.yahoo.com/rss/topstories";
$curlHandle = curl_init ( $val );
$filePointer = fopen ( $tmpFile, "w" );
curl_setopt ( $curlHandle, CURLOPT_FILE, $filePointer );
curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2');
curl_setopt($curlHandle, CURLOPT_ENCODING , "gzip");
curl_setopt($curlHandle, CURLOPT_TIMEOUT,5);
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec ( $curlHandle );
curl_close ( $curlHandle );
fclose ( $filePointer );

$linesArr = file ( $tmpFile );
foreach ( $linesArr as $eachLine ) {
    echo ($eachLine);
}

我希望这会有所帮助..如果您需要更多信息,请告诉我

于 2012-04-11T02:32:25.880 回答
0

如果你有httpfile_*_contents 的包装器支持:

header('Access-Control-Allow-Origin: *');
$tmpFile = 'out.txt';
$content = file_get_contents("http://rss.news.yahoo.com/rss/topstories");
file_put_contents($tmpFile, $content);

echo $content;
于 2012-04-11T02:35:41.073 回答