Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

HTTP requests with Guzzle in PHP

Tuesday, October 11th, 2016 by Servage

guzzleGuzzle is an HTTP client that makes it possible to send HTTP requests using PHP. This allows you to communicate with other services, such as APIs, without resorting to external applications or languages other than PHP. Guzzle supports many HTTP methods such as GET and POST requests, custom header fields and and even file uploads. GET and POST requests are the most common types of requests, so let’s see how they work with Guzzle.

Let’s start with the easiest request type: GET requests. Whenever you visit a website, your browser sends a GET request to a server. With Guzzle, you can send a GET request with PHP instead and optionally fetch the output that the request generates.

Installing Guzzle and setting up the client

To send a GET request, you first need an instance of the Client object that is part of the Guzzle library. The Client is like a web browser: you can control it and command it to send HTTP requests. To create an instance of it, use this code as a reference:

$client = new GuzzleHttp/Client();

Note that you must have Guzzle installed for this to work. If you are familiar with Composer, you can install it with “composer require guzzlehttp/guzzle”. If you are not using Composer, have a look at the Guzzle documentation for alternative installation methods.

GET requests

Now that you have the Client ready, you can tell it to start sending HTTP requests. This is as simple as calling the request() method of the object:

$client->request(“GET”, “http://swapi.co/api/people/1”);

As the first argument to the method, pass the HTTP request type you want to use. In this case we are using a GET request. The second argument is the URL to request. In this case it is the Star Wars API, which is a great public API for testing your code. If you want to get a response back from the request, simply assign it to a variable:

$response = $client->request(“GET”, “http://swapi.co/api/people/1”);

To get the main content of the response, use the getBody() method of the $response object:

echo $response->getBody()

POST Requests

Sending a POST request is very similar to a GET request. The only difference is that you can give a set of form parameters as the third argument. Let’s use httpbin, another API testing service, this time:

$client->request(“POST”, “http://httpbin.org/post”, [ 'form_params' => [ 'username’ => 'foo, 'password’=> 'bar’ ] ] );

Also, remember to change the request type to POST in the first argument. Submitting a request like this is the same as filling out a form with a user name and password field.

HTTP requests with Guzzle in PHP, 4.3 out of 5 based on 6 ratings
Categories: Software & Webapps, Tips & Tricks

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.