Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Difference between Curl and HttpRequest

Tags:

http

post

php

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)

I have two ways of doing it

  • Curl (http://us.php.net/manual/en/book.curl.php)
  • PHP HTTP using the HTTPRequest (http://us.php.net/manual/en/book.http.php)

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

like image 665
The Unknown Avatar asked May 15 '09 17:05

The Unknown


People also ask

What is difference between cURL and REST API?

cURL is a command line tool for transfering data via URLs. When it comes to REST APIs, we can use Postman as a GUI (graphical user interface) and cURL as a CLI (command line interface) to do the same tasks.

Is cURL a HTTP client?

cURL: It stands for “client URL” and is used in command line or scripts to transfer data. It is a great tool for dealing with HTTP requests like GET, POST, PUT, DELETE, etc.

What is cURL in PHP with example?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.


3 Answers

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

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.

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

like image 180
Frank Farmer Avatar answered Oct 19 '22 17:10

Frank Farmer


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

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.

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.

like image 23
JayTee Avatar answered Oct 19 '22 16:10

JayTee


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

  • 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.

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.

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

like image 5
palako Avatar answered Oct 19 '22 16:10

palako