开发者

What are nice use cases for cURL in PHP?

开发者 https://www.devze.com 2023-02-10 01:51 出处:网络
It\'s evident that the cURL functions are very widely used. But why is that? Is it really only because the extension is mostly enabled per default?

It's evident that the cURL functions are very widely used. But why is that? Is it really only because the extension is mostly enabled per default?

While I can certainly relate to not introducing 3rd party libraries over builtins (DOMDocument vs phpQuery), using curl appears somewhat odd to me. There are heaps of HTTP libraries like Zend_Http or PEAR Http_Request. And despite my disdain for needless object-oriented interfaces, the pull-parameter-procedural API of curl strikes me as less legible in comparison.

There is of course a reason for that. But I'm wondering if most PHP developers realize what else libcurl can actual开发者_运维技巧ly be used for, and that it's not just a HTTP library?

Do you have examples or actual code which utilizes cURL for <any other things> it was made for?

Or if you just use it for HTTP, what are the reasons. Why are real PHP HTTP libraries seemingly avoided nowadays?


I think this would be related to why do people use the mysql functions instead of mysqli (more object oriented interface) or take a step further and use a data abstraction layer or PDOs.

HTTP_Request2 says that there is a cURL adapter available to wrap around PHP's cURL functions.

Personally a lot of the PEAR extensions I have tried out, I haven't been that impressed with (and I feel less confident with PEAR libraries that are sitting in alpha that haven't been updated in a long time). Whereas the HTTP_Request2 Library does look quite nice

I for one would have used cURL without thinking of looking at a possible PEAR library to use. So thanks for raising my awareness.


The libraries you mentioned aren't default, and from my experience in PHP, I prefer to use less of such libraries; they enable a broader attack surface, decrease reliability, open to future modification/deprecation more than PHP itself.

Then, there's the sockets functionality which, although I've used some times, I prefer to rely on a higher level approach whenever possible.

What have I used CURL for?

As some may know, I'm currently working on a PHP framework. The communication core extension (appropriately called "connect") makes use of CURL as it's base.

I've used it widely, from extracting favicons form websites (together with parser utilities and stuff) to standard API calls over HTTP as well as the FTP layer when PHP's FTP is disabled (through stream wrappers) - and we all know native PHP FTP ain't that reliable.


Functional reasons as mentioned in the comments:

  • It's very old, [widely used and] well tested code, works reliably
  • is usually enabled by default
  • allows very fine grained control over the details of the request.
    • This might need expanding. By nature of the common-denominator protocol API cURL might provide features that plain HTTP libraries in PHP can't...

Historic reasons:

  • curl used to be the only thing that could handle cookies, POST, file uploads...
  • A lot of curl use probably comes from tutorials that pre-date PHP 5.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号