Curl 和 HttpRequest 的 PHP 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/869927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:09:33  来源:igfitidea点击:

PHP Difference between Curl and HttpRequest

phphttppost

提问by The Unknown

I have a need to do RAW POST (PUT a $var) requests to a server, and accept the results from that page as a string. Also need to add custom HTTP header information (like x-example-info: 2342342)

我需要对服务器执行 RAW POST (PUT a $var) 请求,并将该页面的结果作为字符串接受。还需要添加自定义的HTTP头信息(比如x-example-info:2342342)

I have two ways of doing it

我有两种方法

What are the differences between the two? what's more lean? faster? Both seem pretty much the same to me...

两者之间有什么区别?什么更瘦?快点?两者在我看来几乎一样......

回答by Frank Farmer

Curl is bundled with PHP, HTTPRequest is a separate PECL extension.

Curl 与 PHP 捆绑在一起,HTTPRequest 是一个单独的 PECL 扩展。

As such, it's much more likely that CURL will be installed on your target platform, which is pretty much the deciding factor for most projects. I'd only consider using HTTPRequest if you plan to only ever install your software on servers you personally have the ability to install PECL extensions on; if your clients will be doing their own installations, installing PECL extensions is usually out of the question.

因此,CURL 更有可能安装在您的目标平台上,这几乎是大多数项目的决定因素。如果您打算只在您个人有能力安装 PECL 扩展的服务器上安装您的软件,我只会考虑使用 HTTPRequest;如果您的客户将进行自己的安装,则安装 PECL 扩展通常是不可能的。

This pageseems to suggest that HTTPRequest uses CURL under the hood anyway. Sounds like it might offer a slightly more elegant interface to curl_multi_*(), though.

该页面似乎表明 HTTPRequest 无论如何都在幕后使用了 CURL。不过,听起来它可能会为 curl_multi_*() 提供一个稍微优雅的界面。

回答by JayTee

HTTPRequest (and the PECL extension) is built on libcurl.

HTTPRequest(和 PECL 扩展)建立在 libcurl 之上。

http://us.php.net/manual/en/http.requirements.php

http://us.php.net/manual/en/http.requirements.php

The HTTPRequest is really just an easier/more syntactically friendly way to perform the same task.

HTTPRequest 实际上只是执行相同任务的一种更简单/语法更友好的方式。

As Frank Farmer mentioned, you're more likely to have a target platform with curl already installed and could have difficulty getting the PECL library installed by the hosting provider.

正如 Frank Farmer 所提到的,您更有可能拥有一个已安装 curl 的目标平台,并且可能难以让托管提供商安装 PECL 库。

回答by palako

The HTTPRequest is "kind of" a wrapper for curl. This two quotes from the manual should give you a clue:

HTTPRequest 是 curl 的“某种”包装器。手册中的这两句话应该给你一个线索:

  • It provides powerful request functionality, if built with CURL support. Parallel requests are available for PHP 5 and greater.

  • The extension must be built with ? libcurl support to enable request functionality (--with-http-curl-requests). A library version equal or greater to v7.12.3 is required.

  • 如果使用 CURL 支持构建,它会提供强大的请求功能。并行请求可用于 PHP 5 及更高版本。

  • 扩展必须用 ? libcurl 支持启用请求功能(--with-http-curl-requests)。需要等于或大于 v7.12.3 的库版本。

Said that (and said that I've never used this extension myself), looks like if you want your code to look more object oriented, you can go for this one, but it might be a bit slower, though nothing compared with the external call that you are going to make, so I won't consider performance to make my choice. I would give preference to the fact that curl is built in and this other you have to add it yourself, which is unconvenient and reduces portability in case you want to host your app in a shared environment that you don't control.

说(并说我自己从来没有使用过这个扩展),看起来如果你想让你的代码看起来更面向对象,你可以选择这个,但它可能会慢一点,尽管与外部相比没有什么您将要拨打的电话,所以我不会考虑性能来做出我的选择。我会优先考虑 curl 是内置的,而另一个你必须自己添加它,这很不方便,并且会降低可移植性,以防你想在你无法控制的共享环境中托管你的应用程序。

For the needs that you explained in your question, I would definitely go for curl.

对于您在问题中解释的需求,我肯定会选择 curl。