Reputation: 1291
When i try to parse the google rss feed, i'm getting Network error: 500 internal server error in chrome and firefox developer tool. The first portion of downloading rss feed (with curl) works fine.
I found this example at: http://www.joevasquez.info/development/parsing-xml-feeds-with-php-rss-and-atom/#more-63
Can someone point out on what i'm doing wrong? thank you.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if (function_exists("curl_init")){
$ch=curl_init();
//curl_setopt($ch,CURLOPT_URL,'http://www.joevasquez.info/feed/');
curl_setopt($ch,CURLOPT_URL, 'http://news.google.com/news?hl=en&topic=t&output=rss');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//curl_setopt($ch,CURLOPT_HEADER,0);
$data=curl_exec($ch);
curl_close($ch);
//print($data);
$doc=new SimpleXmlElement($data,LIBXML_NOCDATA);
if (isset($doc->channel)) parseRSS($doc);
function parseRSS($xml){
$cnt=count($xml->channel->item);
for ($i=0;$i<$cnt;$i++){
$url=$xml->channel->item[$i]->link;
$title=$xml->channel->item[$i]->title;
$desc=$xml->channel->item[$i]->description;
echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'<br>';
}
}
?>
</body>
</html>
Upvotes: 1
Views: 1791
Reputation: 1291
Ok, I got it to work. The error from the log:
Fatal error: Call to undefined function parserss()in /home1/aquinto1/public_html/belibook.com/curl/curl3.php on line 17
I cut and pasted function parserRSS before it was called and it works fine now.
The following is my modification:
$doc=new SimpleXmlElement($data,LIBXML_NOCDATA);
function parseRSS($xml){
$cnt=count($xml->channel->item);
for ($i=0;$i<$cnt;$i++){
$url=$xml->channel->item[$i]->link;
$title=$xml->channel->item[$i]->title;
$desc=$xml->channel->item[$i]->description;
echo '<a href="'.$url.'">'.$title.'</a>'.$desc.'<br>';
}
}
if (isset($doc->channel)) parseRSS($doc);
Thank you both!
Upvotes: 1
Reputation: 418
You forgot to close the bracket after the for loop.
for ($i=0;$i<$cnt;$i++){
$url=$xml->channel->item[$i]->link;
$title=$xml->channel->item[$i]->title;
$desc=$xml->channel->item[$i]->description;
}
Upvotes: 3