PHP - 为什么使用 Guzzle 而不是 cURL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36548005/
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
PHP - Why Use Guzzle Instead of cURL?
提问by Cato Minor
In my application, I originally began using cURL to retrieve data from various APIs. Today, I tried using Guzzle to complete the same task. So far, both cURL and Guzzle seem to work equally good.
在我的应用程序中,我最初开始使用 cURL 从各种 API 检索数据。今天,我尝试使用 Guzzle 来完成相同的任务。到目前为止,cURL 和 Guzzle 似乎工作得同样好。
Judging by Github, a lot of people seem to like Guzzle, but I don't really appreciate why.
从Github来看,很多人似乎喜欢 Guzzle,但我不明白为什么。
My question:
我的问题:
For my situation (retrieving data from various APIs), is it preferable to use Guzzle? Will I eventually come to regret it, if I use cURL instead Guzzle (or vice versa)?
对于我的情况(从各种 API 检索数据),使用 Guzzle 是否更可取?如果我使用 cURL 而不是 Guzzle(反之亦然),我最终会后悔吗?
I am using PHP / Laravel.
我正在使用 PHP/Laravel。
回答by Fabio Antunes
Why use Guzzle?
为什么要使用 Guzzle?
First of all Guzzle is an abstraction layer for http request, although it uses cURL by default you can use any other http client that you want:
首先,Guzzle 是 http 请求的抽象层,虽然它默认使用 cURL,但您可以使用任何其他您想要的 http 客户端:
Does Guzzle require cURL?
No. Guzzle can use any HTTP handler to send requests. This means that Guzzle can be used with cURL, PHP's stream wrapper, sockets, and non-blocking libraries like React. You just need to configure an HTTP handler to use a different method of sending requests
Note:Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues. By allowing swappable HTTP handlers, Guzzle is now much more customizable and able to adapt to fit the needs of more developers.
Guzzle 需要 cURL 吗?
不可以。Guzzle 可以使用任何 HTTP 处理程序发送请求。这意味着 Guzzle 可以与 cURL、PHP 的流包装器、套接字和非阻塞库(如 React)一起使用。你只需要配置一个 HTTP 处理程序来使用不同的发送请求的方法
注意:Guzzle 过去只使用 cURL 发送 HTTP 请求。cURL 是一个了不起的 HTTP 客户端(可以说是最好的),当它可用时,Guzzle 将默认继续使用它。这种情况很少见,但一些开发人员没有在他们的系统上安装 cURL 或遇到特定于版本的问题。通过允许可交换的 HTTP 处理程序,Guzzle 现在更加可定制并且能够适应更多开发人员的需求。
Since you are using Laravel, if by any chances you use any email API then by now you already have Guzzle installed. On your Laravel's composer.jsonyou can see a suggestion:
由于您使用的是 Laravel,如果您有机会使用任何电子邮件 API,那么现在您已经安装了 Guzzle。在你的 Laravel 的composer.json你可以看到一个建议:
"suggest": {
...
"guzzlehttp/guzzle": "Required to use the Mailgun and Mandrill mail drivers and the ping methods on schedules (~5.3|~6.0).",
...
}
Another reason will be reusing code, take a look at the comment made by bogdanthe amount of code needed to do a simple http request with cURL. With Guzzle is way more simpler, cleaner, readable and reusable. Its quite easy to create a service that will encapsulate your Http requests.
另一个原因是重用代码,看看bogdan的评论,用 cURL 做一个简单的 http 请求所需的代码量。Guzzle 更简单、更干净、可读和可重用。创建一个服务来封装你的 Http 请求是很容易的。
Guzzle also allows you to do async requests, in a very similar way you do with javascript using promises.
Guzzle 还允许您执行异步请求,这与使用 javascript 使用Promise 的方式非常相似。
Last but not least, tests! It's way more easier to do tests to your API or create Unit tests for your app and mock the http requests with Guzzle than using cURL. More info about the tests here
最后但并非最不重要的是,测试!与使用 cURL 相比,对您的 API 进行测试或为您的应用程序创建单元测试并使用 Guzzle 模拟 http 请求更容易。有关此处测试的更多信息
BUTif you only want to do only a coupleof simple http requests (which doesn't seem to be the case) you don't care about tests and you don't want to have a dependency on Guzzle go for cURL.
但是,如果您只想执行几个简单的 http 请求(似乎并非如此),您就不必关心测试,也不想依赖 Guzzle 来获取 cURL。
回答by DevDonkey
Guzzle is an abstraction layer for HTTP transport which happens to use cURL where available.
Guzzle 是 HTTP 传输的抽象层,它碰巧在可用的地方使用 cURL。
As well as the above, whilst you can do everything yourself with cURL, Guzzle simplifies things enormously, particularly when it comes to debugging.
除了上述内容,虽然您可以使用 cURL 自己完成所有事情,但 Guzzle 极大地简化了事情,尤其是在调试方面。