/    Sign up×
Community /Pin to ProfileBookmark

Getting error in RSS file

Hi,
i am getting error in php file

error is-

Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in C:wampwwwasheeshrs.php:36 Stack trace: #0 C:wampwwwasheeshrs.php(36): SimpleXMLElement->__construct(”, 16384) #1 {main} thrown in C:wampwwwasheeshrs.php on line 36

my code is –

$ch = curl_init(“http://www.hindustantimes.com/RSSFeed/RssFeed.ashx?c=Chunk-HT-UI-HomePage-TopStories/“);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
set_time_limit(120);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
print_r($doc);
if(isset($doc->channel))
{
parseRSS($doc);
}
if(isset($doc->entry))
{
parseAtom($doc);
}
function parseRSS($xml)
{
echo “<strong>”.$xml->channel->title.”</strong>”;
$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.”;
echo “<img src=’bullet.gif’ hspace=’0′ vspace=’0′ border=’0′>&nbsp;”;
echo ‘<font size=2><a href=”‘.$url.'” target=”_blank()”>’.$title.'</a></font><br />’;
}

}
?>

thnks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 15.2009 — Well, the root problem is as stated in the error message. If you want to avoid that ugly result being displayed to the user, you'll need to use a try/catch block to catch the exception and handle it in whatever way you'd like, e.g.:
[code=php]
<?php
// . . .
$data = curl_exec($ch);
curl_close($ch);
try
{
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
// . . .
}
catch Exception($e)
{
echo "<pre>DEBUG:n$ennReceived text:nn$data</pre>";
}
[/code]

After debugging the current issue, you may want to change the actual processing in the catch block to something more user-friendly for the live version.

PS: Please make use of this forum's [noparse][code=php]...[code=php] tags for PHP code sample, and ... tags for non-PHP code[/noparse].
Copy linkTweet thisAlerts:
@asheeshauthorMay 15.2009 — sir thanks for ur help but still it is not running

can u send me the whole code for it.
Copy linkTweet thisAlerts:
@NogDogMay 15.2009 — I just realized I had a misplace parenthesis. The "catch" line should have been:
[code=php]
catch(Exception $e)
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 15.2009 — I ran this, and did not trigger the exception:
[code=php]
<?php
// show all run-time errors
ini_set('display_errors', 1); // change to 0 for live version
error_reporting(E_ALL);

$ch = curl_init("http://www.hindustantimes.com/RSSFeed/RssFeed.ashx?c=Chunk-HT-UI-HomePage-TopStories/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
set_time_limit(120);
$data = curl_exec($ch);
curl_close($ch);
// use try/catch to handle exceptions
try
{
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
print_r($doc);
if(isset($doc->channel))
{
parseRSS($doc);
}
if(isset($doc->entry))
{
parseAtom($doc);
}
}
catch(Exception $e)
{
echo "<pre>DEBUG:n".$e->get_message()."nnReceived text:nn".
htmlspecialchars($data)."</pre>";
}
function parseRSS($xml)
{
echo "<strong>".$xml->channel->title."</strong>";
$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.'';
echo "<img src='bullet.gif' hspace='0' vspace='0' border='0'>&nbsp;";
echo '<font size=2><a href="'.$url.'" target="_blank()">'.$title.'</a></font><br />';
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@asheeshauthorMay 15.2009 — now the error is -

Fatal error: Call to undefined method Exception::get_message() in C:wampwwwasheeshtest.php on line 36

plz tell me the right code for it
Copy linkTweet thisAlerts:
@NogDogMay 15.2009 — Sorry, that should be [b]$e->getMessage()[/b] (not [b]$e->get_message()[/b]).
Copy linkTweet thisAlerts:
@asheeshauthorMay 18.2009 — sir thanks for your help but y it is showing a blank page in times of india RSS and when i use ddinews rss it shows all the RSS.

plz help me out in showing all the rss of times of india

thanks
×

Success!

Help @asheesh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...