www.webdeveloper.com
Results 1 to 8 of 8

Thread: Getting error in RSS file

  1. #1
    Join Date
    Apr 2009
    Posts
    9

    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:\wamp\www\asheesh\rs.php:36 Stack trace: #0 C:\wamp\www\asheesh\rs.php(36): SimpleXMLElement->__construct('', 16384) #1 {main} thrown in C:\wamp\www\asheesh\rs.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

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    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.:
    PHP Code:
    <?php
    // . . .
    $data curl_exec($ch);
    curl_close($ch);
    try
    {
       
    $doc = new SimpleXmlElement($dataLIBXML_NOCDATA);
       
    // . . .
    }
    catch 
    Exception($e)
    {
       echo 
    "<pre>DEBUG:\n$e\n\nReceived text:\n\n$data</pre>";
    }
    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 [php]...[php] tags for PHP code sample, and [code]...[/code] tags for non-PHP code.
    Last edited by NogDog; 05-15-2009 at 05:19 AM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2009
    Posts
    9

    still having problem

    sir thanks for ur help but still it is not running

    can u send me the whole code for it.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    I just realized I had a misplace parenthesis. The "catch" line should have been:
    PHP Code:
    catch(Exception $e
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    I ran this, and did not trigger the exception:
    PHP Code:
    <?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($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_HEADER0);
    set_time_limit(120);
    $data curl_exec($ch);
    curl_close($ch);
    // use try/catch to handle exceptions
    try
    {
       
    $doc = new SimpleXmlElement($dataLIBXML_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()."\n\nReceived text:\n\n".
            
    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 />';
       }
    }
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Apr 2009
    Posts
    9

    call to undefined method

    now the error is -

    Fatal error: Call to undefined method Exception::get_message() in C:\wamp\www\asheesh\test.php on line 36

    plz tell me the right code for it

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Sorry, that should be $e->getMessage() (not $e->get_message()).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Apr 2009
    Posts
    9

    now it shows a blank page.

    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles