Which is better approach between fsockopen and curl?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 08:23:12

问题


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?


回答1:


i would recommend you curl, because of the configurable options. e.g. ssl




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/5933682/which-is-better-approach-between-fsockopen-and-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!