Rss reader in PHP

时光毁灭记忆、已成空白 提交于 2020-01-24 17:17:10

问题


 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);
}

The program supposed to fetch all the materials from yahoo rss sites and output them into the tmpFile.

After executed the program, I opened up the tmpFile.txt. It shows

c1.ops.sp1.yahoo.com uncompressed/chunked Wed Apr 11 01:46:41 UTC 2012 -->

This doesn't look right. I pasted the url http://rss.news.yahoo.com/rss/topstories there are plenty of materials returned.


回答1:


What you needed was curl_setopt($curlHandle, CURLOPT_ENCODING , "gzip"); .... yahoo uses compression for its rss feed ...

Additional Information Include .

A. CURLOPT_USERAGENT.... Its nice if you don;t what to start looking like spam

B. CURLOPT_TIMEOUT... Just for effeciency

C. CURLOPT_FOLLOWLOCATION .. Becasue of issues with clean URL and routes

Working Code

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);
}

I hope this helps .. let me know if you need anything more




回答2:


If you have http wrapper support with file_*_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;


来源:https://stackoverflow.com/questions/10099062/rss-reader-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!