#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    PHP RSS Grabber very slow


    Hi,

    I have a script that grabs the latest two posts from my blog (on the same server) and displays them on my website.

    I am currently only testing this locally, but it still can takes up to 6 seconds to load due to this script, which I cannot understand why.

    Here is the script:

    PHP Code:
                      <?php
                        $rss 
    = new DOMDocument();
                        
    $rss->load('http://localhost/lmb/blog/feed');
                        
    $feed = array();
                        foreach (
    $rss->getElementsByTagName('item') as $node) {
                            
    $item = 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($feed$item);
                        }
                        
    $limit 2;
                        for(
    $x=0;$x<$limit;$x++) {
                            
    $title str_replace(' & '' &amp; '$feed[$x]['title']);
                            
    $link $feed[$x]['link'];
                            
    $description $feed[$x]['desc'];
                            
    $description substr($description0240);                        
                            
    $date date('l F d, Y'strtotime($feed[$x]['date']));
                            echo 
    '<strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
                            echo 
    '<p>'.$description.'</p>';
                        }
                      
    ?>
    The script works great, but I can't see why it would be so slow, especially locally.

    All help is appreciated!
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,871
    Rep Power
    6351
    How long does http://localhost/lmb/blog/feed take to load?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    It's instant. At most a second. I had this in mind and tried to see if sometimes it did arbitrarily take longer, but there were no significant results from doing this (i.e. it was always below 1 second).

    Also, what I did as a test was save the RSS feed and put it in the root directory and used that URL. This meant it was a static file that wasn't going to change (rather than a file generated on request) and it was still slow, which I why I presumed it was a code issue.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,974
    Rep Power
    375
    how about if you take away the two for & foreach loops and just dump the rss..

    (just to see maybe for & foreach timings)

    or alternatively, you could put timings in between each action and echo out to see which one takes longer

IMN logo majestic logo threadwatch logo seochat tools logo