Which is better approach between fsockopen and curl?
Solution 1
I would recommend using PHP's stream contexts with the built in functions: http://us3.php.net/manual/en/book.stream.php . Full HTTP/S functionality and integrates nicely with fopen
/file_get_contents
functions. You can (for example) do a POST like this:
$chunk = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".FACEBOOK_APP_ID."&client_secret=".FACEBOOK_SECRET."&grant_type=client_credentials");
if ($request_ids && $chunk) {
$cookie = explode('=', $chunk);
if (count($cookie) == 2) $cookie = $cookie[1];
else $cookie = $cookie[0];
// flush it
foreach ($request_ids as $request_id) {
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'content' => 'method=DELETE',
'user_agent' => "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)",
'max_redirects' => 0
)
));
@file_get_contents('https://graph.facebook.com/' . $request_id . '?access_token=' . $cookie, false, $context);
}
}
This code logs into Facebook, fetches an App Login token and then uses a secure HTTP POST to delete a number of objects using the graph API.
If you need to do fancier things, you can as well.
$context = stream_context_create(array('http' => array(
// set HTTP method
'method' => 'GET',
'user_agent' => "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)",
'max_redirects' => 0
)));
// extract the cookies
$fp = fopen(URL, "r", false, $context);
$meta = stream_get_meta_data($fp);
$headers = $metadata['wrapper_data'];
fclose($fp);
Will log Will fetch you the headers returned by the URL. No external libraries required.
Solution 2
Neither. Not directly, I mean.
Writing and parsing HTTP headers over the bare metal of a socket is insane, and I find curl's API to be downright offensive.
Take a look at PEAR's HTTP_Request2, it's probably even installed on your machine. And if not, you can just bundle it in with your code -- it's BSD licensed. It wraps either sockets or curl, and provides a relatively sane HTTP interface.
Solution 3
Use Curl when you have to handle http protocol, and socket when you need a more generic access to non http server.
Solution 4
I'm looking into this right now and came across the following page which gives code for testing different options and producing speed outputs. Very interesting.
http://www.hashbangcode.com/blog/quickest-way-download-web-page-php
Related videos on Youtube
Gaurav
I am a Joomla Extension Developer. I am involved in development of Payplans(a joomla subscription system) and PayCart (a Joomla Shopping Cart). I was also involved in other popular extensions like JoomlaXi Profile Type, JoomlaXi User Search, JoomlaXi Captcha, JomSocial Profile Completeness, Admin Approval etc.
Updated on May 31, 2022Comments
-
Gaurav almost 2 years
I am creating an app for Automated Recurring Billing.
Please let me know which option should I opt for sending the request to server- fsockeopen
- curl
and why one is better than another?
-
Chameron over 12 yearsSo what should I do in the case allow_url_fopen is false ?
-
John over 7 yearsI really like the approach made here but it has a serious flaw. The testing has great fluctuations in timing, it's so serious that the whole meassurement result is almost useless. Server answer times, momentary server lag as well as momentary quality of the route to the server all play a large role in the tests. The real conclusion I took out of the test is that it's almost not relevant for HTTP queries as the random various lags are a multiple of any API performance difference.