What is the Fastest Way to parse RSS feed in PHP?
I have pulled 7 RSS feed and parse in our website. But the parse time is very slow, what is the best way to parse RSS feed in PHP.
Also is there a way to GZip for faster parse time?
I use this GZip in my .htaccess file but no difference
Here's the GZip script in my .htaccess file
# compress text, html, javascript, css, xml: AddOutputFilterByType
DEFLATE text/plain text/html text/xml text/css application/xml
application/xhtml+xml application/rss+xml application/javascript
application/x-javascript
Here's my PHP code to parse RSS feeds
<?php
function getFeed($feed_url){
$content = file_get_contents($feed_url);
$x = new SimpleXMLElement($content);
for ($i=0; $i<=4; $i++){
$entry = $x->channel->item[$i];
// if promotion price is set, then use the promotion price instead of Normal Price
$normal = $entry->NormalPrice;
$promo = $entry->PromotionPrice;
// Get the links and titles
$image = $entry->Image->Url;
$link = $entry->link;
$title = $entry->title;
if((isset($promo)) && ($promo > 0)){
$price = 'R '.number_format(trim($promo), 2);
}else if($normal > 0 && $promo == 0){
$price = 'R '.number_format(trim($normal), 2);
}else if((empty($normal) && empty($promo)) || (($normal == 0) && ($promo == 0))){
$price = 'Out Of Stock';
}
echo '<div class="rssImages">';
echo '<div class="imageCover">';
echo '<a href="'.$link.'"&开发者_如何学Gogt;<img src="'.$image.'" /></a><br /><br />';
echo '</div>';
echo '<div class="rssCntImg">';
echo '<a href="'.$link.'" id="rsslinks">'.$title.'</a> <br />';
echo '</div>';
echo '<strong>'.$price."</strong>";
echo '<label class="cleared"></label>';
echo '</div>';
}
}
?>
any help/suggestion will be appreciated.
It seems you are parsing your feeds online, on each and every user request.
It always would be slow. It is network, you know.
The only sensible way of reading RSS feeds is asynchronous one. One script to parse them regularily and update the local database and another to show entries from the local database.
And, you know, gzipping output has very little to do with parsing inbound data. Go figure.
I'm not sure that Gzip could have a positive influence on the parse time, it will decrease the download time but that's all.
You should consider using the SAX parser instead of simplexml :
Php Doc about SAX Parser
An example
SAX parser is faster than simplexml on big file (if it well used). The memory use by the sax parser is lower too.
have a look on this answer
Summary of comments and answers:
- Make sure you only parse when needed, check for updates separately and serve information from a local DB on page requests.
- Make sure the latency isn't due to slow response from the remote servers (again, this will be alleviated by caching).
- Unless the feeds are HUGE, any XML parser will do, and if they are HUGE, that might be the cause of the latency...
- GZip, as stated is server side, you can specify that the client (your script) accepts gzip, using for instance cURL, but usage of cURL is a question that should be asked separately. It will in any event do nothing to decrease parse time.
精彩评论