PHP - 为什么使用 Guzzle 而不是 cURL?
PHP - Why Use Guzzle Instead of cURL?
在我的应用程序中,我最初开始使用 cURL 从各种 API 检索数据。今天,我尝试使用 Guzzle 来完成同样的任务。到目前为止,cURL 和 Guzzle 似乎都同样有效。
根据 Github 判断,很多人似乎都喜欢 Guzzle,但我不太明白为什么。
我的问题:
对于我的情况(从各种 API 检索数据),使用 Guzzle 更好吗?如果我使用 cURL 而不是 Guzzle(反之亦然),我最终会后悔吗?
我正在使用 PHP / Laravel。
Guzzle 是 HTTP 传输的抽象层,它恰好在可用的情况下使用 cURL。
除上述内容外,虽然您可以使用 cURL 自行完成所有操作,但 Guzzle 极大地简化了事情,尤其是在调试方面。
为什么要使用 Guzzle?
首先,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.
因为您正在使用 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).",
...
}
另一个原因是重用代码,请查看 bogdan 使用 cURL 执行简单 http 请求所需的代码量。 Guzzle 更简单、更清晰、可读和可重用。创建封装您的 Http 请求的服务非常容易。
Guzzle 还允许您执行 async requests,其方式与使用 promises javascript 的方式非常相似。
最后但同样重要的是,测试!与使用 cURL 相比,使用 Guzzle 对您的 API 进行测试或为您的应用程序创建单元测试并模拟 http 请求要容易得多。有关测试的更多信息 here
但是 如果你只想几个 简单的 http 请求(这似乎不是这种情况)你不关心测试,你不想依赖 Guzzle 去 cURL。
Guzzle 之于 cURL 就像 axios 之于 XMLHttpRequest。
在我的应用程序中,我最初开始使用 cURL 从各种 API 检索数据。今天,我尝试使用 Guzzle 来完成同样的任务。到目前为止,cURL 和 Guzzle 似乎都同样有效。
根据 Github 判断,很多人似乎都喜欢 Guzzle,但我不太明白为什么。
我的问题:
对于我的情况(从各种 API 检索数据),使用 Guzzle 更好吗?如果我使用 cURL 而不是 Guzzle(反之亦然),我最终会后悔吗?
我正在使用 PHP / Laravel。
Guzzle 是 HTTP 传输的抽象层,它恰好在可用的情况下使用 cURL。
除上述内容外,虽然您可以使用 cURL 自行完成所有操作,但 Guzzle 极大地简化了事情,尤其是在调试方面。
为什么要使用 Guzzle?
首先,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.
因为您正在使用 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).",
...
}
另一个原因是重用代码,请查看 bogdan 使用 cURL 执行简单 http 请求所需的代码量。 Guzzle 更简单、更清晰、可读和可重用。创建封装您的 Http 请求的服务非常容易。
Guzzle 还允许您执行 async requests,其方式与使用 promises javascript 的方式非常相似。
最后但同样重要的是,测试!与使用 cURL 相比,使用 Guzzle 对您的 API 进行测试或为您的应用程序创建单元测试并模拟 http 请求要容易得多。有关测试的更多信息 here
但是 如果你只想几个 简单的 http 请求(这似乎不是这种情况)你不关心测试,你不想依赖 Guzzle 去 cURL。
Guzzle 之于 cURL 就像 axios 之于 XMLHttpRequest。