Reputation: 906
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:
Besides simpler and shorter code, is there any other advantage of PECL_HTTP over cURL?
Upvotes: 7
Views: 8883
Reputation:
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);
}
Upvotes: 11