PHP中的RSS阅读器
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未压缩/分块星期三Apr 11 01:46:41 UTC 2012-->
这看起来不太对劲。我粘贴了urlhttp://rss.news.yahoo.com/rss/topstories有很多资料返回。
解决方案
您需要的是curl_setopt($curlHandle, CURLOPT_ENCODING , "gzip");
.yahoo
将compression
用于其rss
源.
其他信息包括。
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);
}
我希望这会有帮助..如果您还需要什么,请告诉我
相关文章