Guzzle is a powerful PHP HTTP client library that simplifies sending HTTP requests and integrating with web services. It supports modern PHP features like PSR-7 standards, promises for asynchronous requests, and extensive middleware capabilities.
Prerequisites
Before installing Guzzle, ensure you have: - PHP 7.2.5 or higher (PHP 8.0+ recommended) - Composer - PHP's dependency manager
Quick Installation
For existing Composer projects, simply run:
composer require guzzlehttp/guzzle
Complete Installation Guide
1. Install Composer (if needed)
If Composer isn't installed, download it from the official website or use these commands:
Linux/macOS:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Windows: Download and run the Composer-Setup.exe installer.
2. Initialize Your Project
For new projects, create a composer.json
file:
composer init
Follow the interactive prompts, or create a minimal composer.json
manually:
{
"name": "your-vendor/project-name",
"require": {
"php": "^7.2.5"
}
}
3. Install Guzzle
Add Guzzle to your project dependencies:
composer require guzzlehttp/guzzle
This command will:
- Add Guzzle to your composer.json
file
- Download Guzzle and its dependencies to the vendor/
directory
- Generate the Composer autoloader
4. Include the Autoloader
In your PHP files, include Composer's autoloader:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
Basic Usage Examples
Simple GET Request
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
try {
$response = $client->request('GET', 'https://api.github.com/users/octocat');
echo "Status Code: " . $response->getStatusCode() . "\n";
echo "Content-Type: " . $response->getHeaderLine('content-type') . "\n";
echo "Body: " . $response->getBody() . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
POST Request with JSON Data
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$data = [
'name' => 'John Doe',
'email' => 'john@example.com'
];
try {
$response = $client->request('POST', 'https://api.example.com/users', [
'json' => $data,
'headers' => [
'Authorization' => 'Bearer your-api-token',
'Accept' => 'application/json'
]
]);
$result = json_decode($response->getBody(), true);
print_r($result);
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Client with Base URI and Default Options
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'https://api.example.com/',
'timeout' => 30.0,
'headers' => [
'User-Agent' => 'MyApp/1.0',
'Accept' => 'application/json'
]
]);
// Now you can use relative URIs
$response = $client->request('GET', 'users/123');
Verification
Check Installation
Verify Guzzle is installed correctly:
composer show guzzlehttp/guzzle
Test Your Setup
Create a simple test file:
<?php
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
echo "Guzzle version: " . GuzzleHttp\Client::MAJOR_VERSION . "\n";
$client = new Client();
$response = $client->request('GET', 'https://httpbin.org/get');
if ($response->getStatusCode() === 200) {
echo "✅ Guzzle is working correctly!\n";
} else {
echo "❌ Something went wrong.\n";
}
Troubleshooting
Common Issues
Missing extensions:
# Install required PHP extensions
sudo apt-get install php-curl php-json php-mbstring
SSL certificate issues:
$client = new Client([
'verify' => false // Only for development/testing
]);
Memory or timeout issues:
$client = new Client([
'timeout' => 60,
'stream' => true // For large responses
]);
Next Steps
With Guzzle installed, you can explore advanced features: - Asynchronous requests with promises - Middleware for request/response modification - Stream handling for large files - Retry mechanisms and circuit breakers
Check the official Guzzle documentation for comprehensive guides and examples.