Performance of file_get_contents()

Asked

Viewed 275 times

2

I’m taking the content of an RSS Feed generated by Wordpress, and the same has brought the information with a little delay (according to Google Pagespeed Insights, is having a delay of 1 to 2.5s).

I wonder if this is due to my code snippet or the RSS feed itself.

PS: When commenting on this code snippet, the site flows normally.

$feed = file_get_contents('http://blog.exal.com.br/feed');
$rss = new SimpleXmlElement($feed);

$contadorNoticias = 0;

foreach($rss->channel->item as $post) {

    if($contadorNoticias < 3){
        $imagemNoticia = "/common/function/thumb.php?largura=730&amp;altura=405&amp;crop=true&amp;imgCrop=center&amp;url=".$post->enclosure['url'];
        $listaNoticia .= "
        <div class=\"col-sm-4\">
            <a class=\"itemOpacity\" href=\"".$post->link."\" title=\"Ir para: Notícias > ".$post->title."\">
                <img src=\"".$imagemNoticia."\" alt=\"Imagem: ".$post->title."\" class=\"img-responsive\" />
                <h2>".$post->title."</h2>
                <p>".str_truncate(strip_tags($post->description),220)." [...]</p>
            </a>
        </div>"; 
    }
    else{
        break;
    }

    $contadorNoticias++;
}

echo $listaNoticia;

1 answer

4


This is only because you are downloading an external content. Whenever you run this it will connect with the http://blog.exal.com.br/feed, will search the IP address and connect, then will wait for the response of the website and get the data, download, then run the rest of the code based on what was obtained, simple like this.

If you make a:

curl "http://blog.exal.com.br/feed/" -o /dev/null -w "Tempo para conectar: %{time_connect}\nTempo para começar transferencia: %{time_starttransfer}\nTempo Total: %{time_total}"

In Windows use: -o null, to remove the alert from Failed writing body because there is no /dev/null on Windows. :S

This will inform you how long it took for your server (or any device) to connect and get the data from the other website.


In my tests:

Tempo para conectar: 0.154
Tempo para começar transferencia: 1.501
Tempo Total: 1.878

Tempo para conectar: 0.031
Tempo para começar transferencia: 1.406
Tempo Total: 1.406

Tempo para conectar: 0.137
Tempo para começar transferencia: 1.184
Tempo Total: 1.557

...

  • Tempo para conectar (time_connect): indicates the time it takes for CURL to create the TCP connection to properly connect to the server (or proxy).

  • Tempo para começar transferencia (time_starttransfer): indicates the time it takes for CURL to receive the first byte of response, this is the time it took for CURL to start receiving some data.

  • Tempo Total (time_total): indicates the total time it took to obtain the result.


Where is the problem?

The main problem is the website that generates the feed itself (http://blog.exal.com.br/feed), since it takes on average 1350 milliseconds to return the information, or to begin to return, so it takes your website to also display such information.

How to solve?

It depends, there is not enough information in the question, the question actually was just "I wonder if this is due to my code snippet or the RSS feed itself." , this is answered above.

However, there are some solutions you can do on your side.

Cache:

Why do you need to connect all the time with the other site? It updates how much in how long?

One option is to create one cronjob and save to an archive /cache/feed and carry it always.

Create a atualizaFeed.php:

$getFeed = file_get_contents('http://blog.exal.com.br/feed');
file_put_contents('alguma/pasta/feed.xml', $getFeed);

Create a cronjob, example: * * * * * php atualizaFeed.php, can use the crontab -e to edit via vi.

You can just forget about PHP and use it directly: * * * * * curl "http://blog.exal.com.br/feed/" -o /alguma/pasta/feed.xml. ;)

Then simply upload the file information that is updated every minute using:

$feed = file_get_contents('alguma/pasta/feed.xml');
$rss = new SimpleXmlElement($feed);

//...

The archive alguma/pasta/feed.xml will always be updated, every minute. Soon, when the user accesses the page will be read the file that is already on your server, this is infinitely faster than waiting for the external server response to each connection.

You can also create a database if you want and update when there are new publications, anyway, several options...

  • Thanks for the detail, @Inkeliz! Gave me idea for another solution. As the WP is in the same hosting site (only separated by the subdomain), I decided instead of bringing the posts via file_get_contents, to make the query directly in the database. And it worked! ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.