开发者

Which is better approach between fsockopen and curl? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 1 year ago.

Improve this question

I am c开发者_StackOverflowreating 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?


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.


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.


Use Curl when you have to handle http protocol, and socket when you need a more generic access to non http server.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜