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 ?
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
Because you will be updating 50 feeds at once, I would strongly suggest using CURL for two reasons:
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With