doulao7998636570 2014-07-21 02:41
浏览 246
已采纳

PHP CURL与PECL_HTTP

In PHP, you can use PHP file stream functions, like file_get_contents() to handle HTTP requests, but to handle complex HTTP communications, cURL is obviously better and more flexible. I've been using cURL for years and it never fails me.

Recently, I tried to test the PECL_HTTP extension, and found that it's even simpler and works great on most HTTP requests, at least at first. However, I still have doubts about the PECL_HTTP extension.

So, is PECL_HTTP as powerful and flexible as cURL? Especially for different kinds of complex HTTP communications? Although PECL_HTTP can shorten your code and make it easier to handle most "regular" HTTP requests, what about more complex HTTP requests?

Here are some disadvantages of PECL_HTTP compared to cURL which I already know:

  1. cURL comes with most PHP and Linux distributions, while the PECL_HTTP extension requires an additional installation.
  2. cURL uses the same coding interface for different languages, while PECL_HTTP version 2 is not even compatible with version 1. A totally different API interface is employed in PECL_HTTP version 2.

Besides simpler and shorter code, is there any other advantage of PECL_HTTP over cURL?

  • 写回答

1条回答 默认 最新

  • doujiang2643 2014-07-21 05:15
    关注

    The PHP curl extension (as well as curl itself) is considered much more mature than the PECL HTTP extension. This is made clear by the fact that the curl extension is part of the PHP core distribution, while the PECL HTTP extension must be downloaded and installed separately.

    If you find the curl interface cumbersome to use for simple requests (I can't blame you), keep in mind that you can implement wrapper functions around it to perform common tasks; for instance, one might write something like:

    function curl_get($url, $options = array()) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt_array($ch, $options);
        return curl_exec($ch);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch