Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL or file_get_contents to update a list of feeds?

I am running a feed reader site, where there will be a lot of RSS around. I will have to synchronize those feeds as often as possible, so I found these two methods of doing it.

1 method : Using CURL

$weblog_name = 'MyBlog';
$weblog_url = 'http://feeds.feedburner.com/myblog';
$ping_url = 'http://ping.feedburner.com';

$request = <<<EOT
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>weblogUpdates.ping</methodName>
<params>
 <param>
  <value>
   <string>$weblog_name</string>
  </value>
 </param>
 <param>
  <value>
   <string>$weblog_url</string>
  </value>
 </param>
</params>
</methodCall>
EOT;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ping_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, trim($request)); 
$result = curl_exec($ch);
curl_close($ch); 

Second Method : file_get_contents

file_get_contents("http://feedburner.google.com/fb/a/pingSubmit?bloglink=http://feeds.feedburner.com/myblog");

My question is which is the better and faster solution to ping at least 50 feeds at once ?

like image 557
EnexoOnoma Avatar asked Sep 21 '11 16:09

EnexoOnoma


3 Answers

Fetching google.com using file_get_contents took (in seconds):

2.31319094
2.30374217
2.21512604
3.30553889
2.30124092

CURL took:

0.68719101
0.64675593
0.64326
0.81983113
0.63956594

This was using the benchmark class from http://davidwalsh.name/php-timer-benchmark

like image 61
EnexoOnoma Avatar answered Oct 11 '22 19:10

EnexoOnoma


Because you will be updating 50 feeds at once, I would strongly suggest using CURL for two reasons:

  1. you can use curl_multi() functions that will allow you to send all 50 requests at once, while file_get_contents() will only go one-by-one. The documentation for these functions is a bit sparse, so I would suggest using a lightweight library - it's much easier to work with. I personally use https://github.com/petewarden/ParallelCurl, but you will find many around.

  2. as you are pinging the services, you do not really need to know the response, I guess (as long as it's HTTP 200). So you could use the CURL option CURLOPT_NOBODY to make it into a HEAD request, thus in response you would get the headers only, too. This should speed up the process even more.

Put it otherwise, file_get_contents might be faster for simple requests, but in this case your situation is not simple. Firing 50 requests without really needed to get the whole document back is not a standard request.

like image 30
Aurimas Avatar answered Oct 11 '22 18:10

Aurimas


Actually i think curl is faster than file_get_contents.

Googling a bit I've found out some benchmarks here in SO: file_get_contents VS CURL, what has better performance?

like image 2
Gianpaolo Di Nino Avatar answered Oct 11 '22 20:10

Gianpaolo Di Nino