PHP HTTP Request
Hi all! As you know, requests are the fundamentals of the web. So knowing how to deal with them will give you superpowers in your fingers! On our way to acquire the force, our today’s concern will be about sending POST data using HTTPRequest PHP class. In fact, there are several ways to achieve that action in PHP, and you may have heard of cURL library that most of the servers have enabled. But we’ll stick to HTTPRequest class for this article. In order to use HttpRequest class and follow the examples codes below, you’ll need the PECL http extension (php_http). Here I use the 1.7.3 version on a PHP 5.4 environment.
The HTTPRequest class is a very powerful one. There are dozens of methods you can use to perform various actions related to Http requests. But to send data, we will use... Its send method (not a big surprise right?).
First let’s look at the constructor of the class.
public HttpRequest::__construct([string $url[, int $request_method = HTTP_METH_GET[, array $options]]])
The constructor will take in three parameters :
- A string $url which will hold the target request URL
- An optional int that indicates the Http request method (whether it’s a PUT, a POST, a GET). We can define it with the help of php constants: METH_PUT, METH_POST, METH_GET
- An optional array which will hold the options
There are a lot of options you can pass to your object, but we’ll only see a few here that I usually need when sending POST data.
- Timeout: The timeout option is the maximum amount of time in seconds that the request may take to complete. The default value is set to infinite so by default, the request may never end. Well, it’s worth to say that it’s a good idea to set one! The value is up to you, and you just need to tweak it to fit your needs.
- Redirect: The redirect option is the maximum number of redirects your request will follow before it gets back to you with a response. The default is 0 and in many situations, trust me it will not work. Most of applications out there will have 2 or 3 redirects for auth and other stuffs so you better set this value to around 4 or 5.
First, create a php file named call.php in the directory you work. Here is the code:
As we want to see if data is actually posted, I just var_dumped the value of POST array.
Then, create the file in which you want to make your tests. Our code will start with instanciating the HTTPRequest object:
$url = 'http://localhost/examples/httprequest/call.php'; $http = new HttpRequest($url, HttpRequest::METH_POST);
As you see in the example, I created a php file named call.php in the same directory of my working script, and pass it as first argument of the class instanciation.
I also passed the constant METH_POST (included in the HttpRequest class, that’s why you have classname:: before) to tell the object it should make POST requests.
Then add this line to actually add POST data to the request:
$http->addPostFields(array( 'firstData' => 'myData', 'secondData' => 'myDataTwo' ));
The method addPostFields() will take an array as argument. It will add POST data entries in a queue unless you add same data keys then it will overwrite it. That means you can call another statement like so:
$http->addPostFields(array( 'thirdData' => 'myData', 'nextData' => 'myDataTwo' ));
Then you can send the data:
$response = $http->send();
I store the returned value directly in a variable to be able to catch the response and eventually print it:
If you worked with the call.php file mentioned above, you should get the POST array dump printed on your browser.
With the options set, my script finally looks like:
$url = 'http://localhost/phparticles/httprequest/call.php'; $http = new HttpRequest($url, HttpRequest::METH_POST); $http->setOptions(array( 'timeout' => 10, 'redirect' => 4 )); $http->addPostFields(array( 'firstData' => 'myData', 'secondData' => 'myDataTwo' )); $http->addPostFields(array( 'thirdData' => 'myData', 'nextData' => 'myDataTwo' )); $response = $http->send(); echo $response->getBody();
Now you can enjoy your super powers!