Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Using PHP to post RSS Content?

    OK so I'm trying to post some news headlines on a site from this rss feed http://feeds.feedburner.com/Newshour...nes?format=xml. I'm really new at this and aren't having any luck. So I found a tutorial that used the below code. I tested it as is and it didn't work. What am I doing wrong? IS there any additional sites that goes into great detail on how to work with rss feeds?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    
    <?php
    
      $doc = new DOMDocument();
      $doc->load('http://www.softarea51.com/rss/windows/Web_Development/XML_CSS_Utilities.xml');
      $arrFeeds = array();
      foreach ($doc->getElementsByTagName('item') as $node) {
        $itemRSS = array ( 
          'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
          'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
          'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
          'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
          );
        array_push($arrFeeds, $itemRSS);
      }
    
    ?>
    
    <?php
    
    echo $arrFeeds();
    
    ?>
    
    </body>
    </html>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can't use echo $arrFeeds(). Array() is a construct, so if you attempted to call it as a variable function its guaranteed to fail.

    So what you have here is a multidimensional array for $arrFeeds. You need to iterate it:
    PHP Code:
    foreach ($arrFeeds AS $item)
    {
        
    printf('Title: %s' PHP_EOL$item['title']);

    For example. Or you can skip the $arrFeeds completely and print from within the foreach on the domdocument.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Here's a way ... with comments

    Try this:
    PHP Code:

    <?php  
    // rss page for Softarea51 -  
    $feed_url "http://www.softarea51.com/rss/windows/Web_Development/XML_CSS_Utilities.xml";

    # INITIATE CURL. 
    $curl curl_init(); 

    # CURL SETTINGS. 
    curl_setopt($curlCURLOPT_URL,"$feed_url"); 
    curl_setopt($curlCURLOPT_RETURNTRANSFER1); 
    curl_setopt($curlCURLOPT_CONNECTTIMEOUT0); 

    # GRAB THE XML FILE. 
    $xmlFile curl_exec($curl); 

    curl_close($curl); 

    # SET UP XML OBJECT.
    # Use either one of these, depending on your PHP features.
    # Comment-out the line you are not using.
    //$xml = new SimpleXMLElement($xmlFile);
    $xml simplexml_load_string($xmlFile); 

    // How many items to display 
    $count 10

    // How many characters from each item 
    // 0 (zero) will show them all. 
    $char 200

    foreach (
    $xml->channel->item as $item) { 
    if(
    $char == 0){ 
    $newstring $item->description

    else{ 
    $newstring substr($item->description0$char); 

    if(
    $count 0){ 
    //in case they have non-closed italics or bold, etc ... 
    echo"</i></b></u></a>\n"
    echo

    <div style='font-family:arial; font-size:.8em;'>  
    <b>{$item->title}</b><br />
    $newstring ... <a href='{$item->guid}'>read more</a> 
    <br /><br /> 
    </div> 
    "
    ;  

    $count--; 
    }  
    ?>

    .


  •  

    Posting Permissions

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