Many a times, we need to make HTTP requests from our server to a URL. In PHP, there are various ways to do so.  Some methods are easier to use than others. Recently, I had to make a HTTP request to a web-service and consume the JSON response. I experimented with a few methods which are out there before settling with a neat little package called Requests.

In this post, I will outline which techniques I used before settling with Requests. 

Background

To experiment with the various methods, I made three web services - one for GET requests, one for simple POST and one for POST with file upload.

The GET and simple POST web-services required the following parameters -
  1. first_name
  2. last_name
  3. city

Both of these web-services return a JSON with the parameters passed. The POST with file upload expects owner, test_file as parameters where test_file is the file that needs to be uploaded. It then returns a JSON with the following fields -
  1. name [Name of the file]
  2. type [Type of the file]
  3. size [Size of the file]
  4. owner

Okay, now back to business.

HTTP Requests with CURL

Making HTTP request with CURL is probably the most popular way to make HTTP requests in PHP. Following are code samples of how to requests the above three web-services.
https://gist.github.com/4177746
Now, we will do the POST request. For brevity, henceforth, I will be showing the main crux of the code instead of the complete code. Hope the code samples will be clear enough to follow. 
https://gist.github.com/4177798
Now, forth with the POST web-service where you can upload a file. Here I am using a local file from my system by giving the complete path of the file.
https://gist.github.com/4178172
All good. But isn't this too much work just to make a simple request. Too many options to set, too many variables to take care of. I didn't like this method at all and started looking for something easier. Then I found the HttpRequest class.

HTTP Requests with HttpRequest

Bad news - HttpRequest is not present by default in PHP. You have to install it. PECL is the preferred way. You can find installation instructions in the link. We will do the exact same requests as above. As always, I will show the main crux of the code (with comments of course).

First, with our simple good ol' GET request.
https://gist.github.com/4178024
Now, with the POST request. It is basically the same as the GET request with very small changes like the METH_GET flag and the method to add POST data to the request.
https://gist.github.com/4178045
Well, that was plain enough. Fortunately, the POST request with the file upload is almost the same but for an extra method call to actually post the file.
https://gist.github.com/4178176
Cool enough. This method is much simpler than using CURL. No more confusing variables and flags to be set. No more explicitly closing the connection. I was content with this till I found out the Requests package. Let's see how does that makes things simpler and succinct.

HTTP Requests with Requests Package

Using the Requests package is simple. Simply clone the repository and include a single file from the package to make it work.

First, with the simple GET request.
https://gist.github.com/4178106
Woah! A single line to make a request. Nothing like it. Same goes for making a POST request and a POST request with file upload. 
https://gist.github.com/4178129
As you see, it is exactly like the GET request. We also used the same method as in GET - just that, we passed another parameter to tell that it is a POST request. Now, with the file upload POST request, it is exactly the same as well except that we make the $data array in the fashion as we did in the CURL method - that is, with a @ symbol for the file name.
https://gist.github.com/4178150
Okay, so it seems that Requests package is really neat to use. It definitely is. We used the same method to make all the requests. No confusing variables and flags, shorter code - that is what everyone wants.
patrick
30/11/2012 05:27:20 am

Have you looked at http_build_query ( http://www.php.net/manual/en/function.http-build-query.php) instead of your foreach?

Reply
2/12/2012 01:42:28 pm

Yes Patrick. http_build_query can be used to greatly simplify the foreach logic.

Reply
7/10/2013 08:18:31 pm

Nice post and nice information too. I read your post. It's really nice and I like your post. Thank you for sharing...........

Reply
24/1/2014 10:49:32 am

Nice post. I read total Post. It’s really nice. Thank you for sharing.........

Reply



Leave a Reply.