DEV Community

Cover image for Setting up proxies in Guzzle: PHP Tutorial
Kev the bur
Kev the bur

Posted on

Setting up proxies in Guzzle: PHP Tutorial

How to Set Up Proxies in Guzzle: A Practical PHP Guide

When working with HTTP clients in PHP, handling proxies often adds unnecessary complexity. Guzzle is a powerful HTTP client that not only simplifies sending requests but also makes integrating proxies straightforward. Whether you're routing traffic through a proxy to mask your IP address, access geo-restricted content, or distribute requests, Guzzle supports it all with minimal setup.

In this guide, we'll dive into how to configure proxies in Guzzle, including setting up and using DataImpulse proxies for smooth and reliable proxy management.

Setting up proxies in Guzzle: PHP Tutorial image 1


Installing Guzzle with Composer

Before diving into proxies, you need Guzzle installed in your PHP project. Composer is the recommended tool to handle this dependency.

Step 1: Install Composer

If Composer is not already on your system, install it with the following commands:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

These commands download the Composer installer, execute it, and move the Composer executable to your system PATH.

Step 2: Install Guzzle

With Composer installed, add Guzzle to your project by running:

composer require guzzlehttp/guzzle
Enter fullscreen mode Exit fullscreen mode

This command downloads Guzzle and the necessary dependencies into your project folder.


Basic Guzzle Usage Without Proxies

Let’s start by making a simple GET request to verify everything is working:

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

use GuzzleHttp\Client;

$client = new Client();
$response = $client->request('GET', 'https://ip-api.com/');
$body = $response->getBody();

echo $body;
Enter fullscreen mode Exit fullscreen mode

This script sends a GET request to ip-api.com, which returns the IP address and other geographical info of the request origin.


Adding a Proxy to Your Guzzle Requests

To route your requests through a proxy, add the proxy option when creating the request. Here’s the format when using an authenticated proxy:

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

use GuzzleHttp\Client;

$client = new Client();

$response = $client->request('GET', 'https://ip-api.com/', [
    'proxy' => 'http://username:password@proxy-host:proxy-port'
]);

echo $response->getBody();
Enter fullscreen mode Exit fullscreen mode

Replace username, password, proxy-host, and proxy-port with your proxy server credentials.

If your proxy does not require authentication, simply provide the host and port:

$response = $client->request('GET', 'https://ip-api.com/', [
    'proxy' => 'http://proxy-host:proxy-port'
]);
Enter fullscreen mode Exit fullscreen mode

Rotating Proxies with Guzzle

When working with multiple proxies, manual rotation of IPs using PHP arrays is possible but inefficient, especially with large proxy pools.

A better approach is to use proxy providers like DataImpulse that offer automatic rotation and management of proxies, so you don’t have to handle IP lists manually.


Using DataImpulse Residential Proxies with Guzzle

DataImpulse offers reliable residential proxies that you can easily integrate with Guzzle. Their proxy server is gw.dataimpulse.com, and the port for HTTP proxies is 823.

Here’s how to configure a request using your DataImpulse proxy credentials:

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

use GuzzleHttp\Client;

$client = new Client();

$response = $client->request('GET', 'http://ip-api.com/', [
    'proxy' => 'http://<YourProxyPlanLogin>:<YourProxyPlanPassword>@gw.dataimpulse.com:823'
]);

echo $response->getBody();
Enter fullscreen mode Exit fullscreen mode

Replace <YourProxyPlanLogin> and <YourProxyPlanPassword> with your actual DataImpulse proxy plan login details.


Simplify Proxy Setup with IP Whitelisting on DataImpulse

DataImpulse also supports IP whitelisting. After whitelisting your IP from their dashboard, you can make proxy requests without including your username and password in the request. This improves security by eliminating credential exposure in your scripts.

Use the following simplified code:

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

use GuzzleHttp\Client;

$client = new Client();

$response = $client->request('GET', 'http://ip-api.com/', [
    'proxy' => 'http://gw.dataimpulse.com:823'
]);

echo $response->getBody();
Enter fullscreen mode Exit fullscreen mode

DataImpulse will automatically verify if the request comes from a whitelisted IP and grant access accordingly.


Website testing with proxies Cost-effective


Final Thoughts

Using proxies in Guzzle is straightforward and powerful, especially when paired with a dependable proxy provider like DataImpulse. Whether you’re handling authenticated proxies, public proxies, or IP whitelisting setups, Guzzle’s flexible options let you integrate proxy support with minimal overhead.

By automating proxy rotations and authentication with providers like DataImpulse, you can focus more on your application logic and less on proxy management.


Related Recognition for DataImpulse

Awards greatest progress 2025

ISO 27001 certification

Newcomer of the Year 2024

DataImpulse New

Top comments (0)