Guzzle for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Guzzle and why is it needed?



Guzzle is a powerful PHP library for working with HTTP requests. It provides a convenient interface for sending HTTP requests, processing responses, working with cookies, sessions, asynchronous requests, and much more. If you work with APIs, scrape web pages, or simply interact with remote servers, Guzzle will become your indispensable tool.



Main features of Guzzle:

  • Support for all HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
  • Working with headers, request body, URL parameters
  • Support for asynchronous requests (Promise-based)
  • Automatic handling of redirects
  • Working with cookies and sessions
  • Middleware for extending functionality
  • Support for PSR-7 (PSR-7 interfaces for HTTP messages)
  • Easy testing and mocking


Guzzle is used in major projects such as Laravel (built-in HTTP client), Drupal, Magento, and many others.



Installation



Guzzle is installed via Composer, the dependency manager for PHP. If you don't have Composer yet, install it from the official website getcomposer.org.



Run the following command in the terminal in the root of your project:



composer require guzzlehttp/guzzle


After installation, include the Composer autoloader in your PHP file:



require 'vendor/autoload.php';


Now you are ready to work with Guzzle.



Quick Start



Let's send a simple GET request to a public API and get data in JSON format.



<?phprequire 'vendor/autoload.php';

use GuzzleHttp\\Client;

$client = new Client();$response = $client->get('https://jsonplaceholder.typicode.com/posts/1');

// Get the response body$body = $response->getBody();

// Convert JSON to array$data = json_decode($body, true);

echo "Title: " . $data['title'] . "";echo "Body: " . $data['body'] . "";?>


This example shows how easy it is to get data from a remote server. Guzzle automatically handles headers, status codes, and the response body.



Main Methods and Functions



1. get() — GET Request

What it does: Sends an HTTP GET request to the specified URL. Used to retrieve data.

Parameters:

  • $uri (string) — Request URL
  • $options (array) — Array of options (headers, query parameters, timeouts, etc.)

Example:

<?php$client = new GuzzleHttp\\Client();$response = $client->get('https://api.github.com/users/octocat', [    'headers' => [        'Accept' => 'application/json',        'User-Agent' => 'Guzzle-Demo'    ],    'query' => [        'page' => 1,        'per_page' => 10    ]]);

echo $response->getStatusCode(); // 200echo $response->getBody();?>


2. post() — POST Request

What it does: Sends a POST request. Used to create new resources or send data to the server.

Parameters:

  • $uri (string) — Request URL
  • $options (array) — Array of options (body, form_params, json, multipart, etc.)

Example:

<?php$client = new GuzzleHttp\\Client();$response = $client->post('https://jsonplaceholder.typicode.com/posts', [    'json' => [        'title' => 'New Post',        'body' => 'Post content',        'userId' => 1    ]]);

echo $response->getStatusCode(); // 201$data = json_decode($response->getBody(), true);print_r(

Recommendations