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 uncompressed / chunked Wed Apr 11 01:46:41 UTC 2012 - >
这看起来不对。我粘贴了网址http://rss.news.yahoo.com/rss/topstories,返回了大量的资料。
答案 0 :(得分:1)
您需要的是curl_setopt($curlHandle, CURLOPT_ENCODING , "gzip");
.... yahoo
使用compression
作为其rss
Feed ......
其他信息包括。
一个。 CURLOPT_USERAGENT
....如果你不喜欢它会很好看;什么开始看起来像垃圾邮件
B中。 CURLOPT_TIMEOUT
...只是为了效率
℃。 CURLOPT_FOLLOWLOCATION
..清除网址和路线问题
工作代码
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);
}
我希望这有帮助..如果您需要更多信息,请告诉我
答案 1 :(得分:0)
如果您对文件_ * _ contents:
有http
包装器支持
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;