开发者

CURL or file_get_contents to update a list of feeds?

开发者 https://www.devze.com 2023-04-06 17:13 出处:网络
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.

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:

  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.


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?


I would recommend considering using curl ... while it might be some development overhead at first sight, it is much more powerful than file_get_contents. especially if you want to fetch multiple feeds, curl multi requests might be worth looking at:

http://php.net/manual/en/function.curl-multi-init.php


If you want flexibility for the future (e.g. Authentication, Cookies, Proxy etc.) then use cURL. The speed is about the same as file_get_contents() judging from benchmarks (some say it's faster)

If you want a quick and easy solution then by all means use file_get_contents(). However, it wasn't built for the purpose for requesting external URL's. Most people swear by cURL for doing any work with external URL's, even simple GET requests.

The only additional work with using cURL is a few extra lines of code, wrap it in a function and you're good to go.


get_file_contents is faster. It does a simple http without any extra instantiations

0

精彩评论

暂无评论...
验证码 换一张
取 消