PHP: Using Guzzle to make URL GET and POST Requests

April 19, 2022

Table of Contents


What is Guzzle

Guzzle is a php library to open external urls and retrieve their content. A common application of this library is for accessing the API (Application Program Interface) of other sites. It can also be used to automate the submission of forms, as well as the uploading or downloading of files to/from other sites.

Php already has an extension, the curl extension, that allows to perform the same functionality, however Guzzle provides an object oriented implementation to make url requests and handle their responses in a more efficient way.

Installation

To install Guzzle we can use the composer command:


php composer.phar require guzzlehttp/guzzle:^7.0

Or, if we are adding Guzzle to an existing project using composer, we will add the guzzle package name to our composer.json file:


{
   "require": {
      "guzzlehttp/guzzle": "^7.0"
   }
}

How to use it

To use Guzzle we would need to first include the vendor/autoload.php script.

Then we will indicate the Guzzle classes that we are going to use, GuzzleHttp\Client and GuzzleHttp\Psr7\Request.


require_once __DIR__ . 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

GET Requests

GET Request

To make a GET request, we start by creating an instance of the Client class.
After that we prepare an instance of the Request class, where we indicate the url we are going to open, and finale we send the request.


<?php
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();

// Prepare Request
$request = new Request('GET', 'https://www.example.com');

// Send Request
$response = $client->send($request);

// Read Response
$response_body = (string)$response->getBody();
var_dump($response_body);

GET Request with Url Parameters

To send parameters to the url, we have two methods to choose from.
We can put the parameters next to the url. Or we can define them in an array that we pass as an option named query on the send method.


<?php
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();

// Method 1
$request = new Request('GET', 'https://www.example.com?param1=value1&param2=value2');
$response = $client->send($request);

// Method 2
$request = new Request('GET', 'https://www.example.com');
$response = $client->send($request, [
    'query' => [
        'param1' => 'value1',
        'param2' => 'value2'
    ]
]);

// Read Response
$response_body = (string)$response->getBody();
var_dump($response_body);

POST Requests

POST Request with Form Fields

To make a POST request that will simulate the submission of a form we will use the form_params option in the send method, and pass the form fields as an associative arrays where the keys are the field names, and the values of the array will be the field values.


<?php
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();

// Prepare Request
$request = new Request('POST', 'https://www.example.com');

// Send Request
$response = $client->send($request, [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'form_params' => [
        'field1' => 'value1',
        'field2' => 'value2'
    ]
]);

// Read Response
$response_body = (string)$response->getBody();
var_dump($response_body);

POST Request with Raw Data

We can also send raw data in a POST request. In this case we will pass the data in the body option.


<?php
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();

// Prepare Request
$request = new Request('POST', 'https://www.example.com');

// Send Request
$response = $client->send($request, [
    'body' => 'ABCDEF',
]);

// Read Response
$response_body = (string)$response->getBody();
var_dump($response_body);

Request Options

When making a request we can specify certain options that will provide additional information or instructions to the request.


Send a Request with Headers

With the headers option we can also send some common headers such as Cache-Control, User-Agent, Accept, and so on.


$response = $client->send($request, [
    'headers' => [
        'Pragma' => 'no-cache',
        'Cache-Control' => 'no-cache',
        'Upgrade-Insecure-Requests' => '1',
        'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Language' => 'en-US,en;q=0.9'
    ]
]);

Other Additional Options

Some other options include the following:

  • verify, if true it will check the SSL certificate, else if false it will not check the certificate. This last option is insecure, and could be used, for instance, when testing a local environment with self-signed certificates.
  • allow_redirects specify if Guzzle will automatically make a request to the url indicated on the Location header of the response.
  • debug, will output information about the connection to the remote url, as well as the response.

$response = $client->send($request, [
    'verify' => false,
    'allow_redirects' => false,
    'debug' => true,
]);


Sending Json data

When you want to send Json data you could use the json option, where you can put the Json data in a associative array.


$response = $client->send($request, [
    'headers' => [
        'Content-Type' => 'application/json'
    ],
    'json' => [
        "field1" => "value1",
        "field2" => "value2",
    ],
]);

Responses

Get the Response Body

To retrieve the response of the request you would use the "getBody" method. This method will return a stream, so you may explicitely cast it to a string or just let it be implicitely cast. Alternatively, you could read it as a stream.


$response_body = (string)$response->getBody();
echo $response_body;

Get a Response Header

You can retrieve the headers of the response with the "getHeader" method. This method returns an array, so you would have to specify the index zero to get the value of the header.


$content_type = $response->getHeader('Content-Type')[0];
echo $content_type;

Get all Response Headers of a specified type

When you have several values for a specific header, you would navigate through the array returned by the "getHeader" method.


$headers = $response->getHeader('Set-Cookie');
foreach ($headers as $header) {
    echo $header . "\n";
}

Get all Response Headers

To get the full headers of the response use the "getHeaders" method. which return them as an array of arrays.


$all_headers = $response->getHeaders();
foreach ($all_headers as $header_name => $headers) {
    foreach ($headers as $header) {
        echo $header_name . ': ' . $header . "\n";
    }
}

Get the Response's Status Code

You can also get the Status Code of the Response with the "getStatusCode" method.


$status_code = $response->getStatusCode();
echo $status_code;

Exceptions

Guzzle can also throw its own exceptions, being a couple of useful ones the ConnectException and RequestException.


<?php
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

use GuzzleHttp\Exception\RequestException;

try {
    $client = new Client();
    
    $request = new Request('GET', 'https://www.example.com');
    
    $response = $client->send($request);
}
catch (RequestException $e) {
    echo $e->getMessage() . "\n";

    var_dump($e->getRequest());

    if ($e->hasResponse()) {
        var_dump($e->getResponse());
    }
}