fsockopen 和curl 之间哪个更好?

发布于 2024-11-05 13:06:52 字数 1431 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

池予 2024-11-12 13:06:52

我建议使用 PHP 的流上下文和内置函数: https://www .php.net/manual/en/book.stream.php 。完整的 HTTP/S 功能,并与 fopen/file_get_contents 函数完美集成。例如,您可以执行如下 POST:

$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);
    }
}

此代码登录 Facebook,获取应用程序登录令牌,然后使用安全 HTTP POST 来使用图形 API 删除多个对象。

如果您需要做更奇特的事情,您也可以。

$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);

将记录 将获取 URL 返回的标头。不需要外部库。

I would recommend using PHP's stream contexts with the built in functions: https://www.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.

绝不服输 2024-11-12 13:06:52

都不是。我的意思是,不直接。

在裸露的套接字上写入和解析 HTTP 标头是疯狂的,而且我发现curl 的 API 是彻头彻尾令人反感的

看看 PEAR 的 HTTP_Request2,它甚至可能安装在您的计算机上。如果没有,您可以将其与您的代码捆绑在一起——它是 BSD 许可的。它包装套接字或curl ,并提供相对合理的HTTP接口。

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.

离旧人 2024-11-12 13:06:52

当你必须处理http协议时使用Curl,当你需要对非http服务器进行更通用的访问时使用socket。

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

寻找一个思念的角度 2024-11-12 13:06:52

我现在正在研究这个问题,并发现了以下页面,其中提供了用于测试不同选项并生成速度输出的代码。非常有趣。

http://www.hashbangcode.com/blog/quickest-way -下载网页-php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文