How to use proxies with PHP Guzzle?

by scrapecrow May 16, 2023

PHP's Guzzle is a popular HTTP client used when web scraping with PHP
and proxies are an integral part of web scraping so here's a quick introduction on how to use proxies with Guzzle:

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

use GuzzleHttp\Client;

// Proxy pattern is:
// scheme://username:password@IP:PORT
// For example:
// no auth HTTP proxy:
$my_proxy = "http://160.11.12.13:1020";
// proxy with authentication
$my_proxy = "http://my_username:my_password@160.11.12.13:1020";
// Note: that username and password should be url encoded if they contain URL sensitive characters like "@":
$my_proxy = 'http://'.urlencode('foo@bar.com').':'.urlencode('password@123').'@160.11.12.13:1020';

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://httpbin.dev',
    // You can set any number of default request options.
    'timeout'  => 2.0,
    'proxy' => [
        'http'  => $my_proxy,      // This proxy will be applied to all 'http' URLs
        'https' => $my_proxy,      // This proxy will be applied to all 'https' URLs
        'https://httpbin.dev' => $my_proxy,  // This proxy will be applied only to 'https://httpbin.dev'
    ]
]);

$response = $client->request('GET', '/ip');
$body = $response->getBody();
print($body);

Guzzle does not support SOCKS proxies and the only available options are php's curl library or buzz.

Note that Guzzle proxy can also be set through the standard *_PROXY environment variables:

$ export HTTP_PROXY="http://160.11.12.13:1020"
$ export HTTPS_PROXY="http://160.11.12.13:1020"
$ export ALL_PROXY="socks://160.11.12.13:1020"

When web scraping, it's best to rotate proxies for each request. For that see our article: How to Rotate Proxies in Web Scraping

Related Articles

Web Scraping With PHP 101

Introduction to web scraping with PHP. How to handle http connections, parse html files for data, best practices, tips and an example project.

PHP
HTTP
DATA-PARSING
XPATH
INTRO
Web Scraping With PHP 101

What is Rate Limiting? Everything You Need to Know

Discover what rate limiting is, why it matters, how it works, and how developers can implement it to build stable, scalable applications.

BLOCKING
CRAWLING
HTTP
What is Rate Limiting? Everything You Need to Know

Guide to Axios Headers

Learn about Javascript's Axios headers. How to configure, update, inspect headers in request and responses, how to set defaults and useful tips

HTTP
NODEJS
Guide to Axios Headers

What is HTTP 401 Error and How to Fix it

Discover the HTTP 401 error meaning, its causes, and solutions in this comprehensive guide. Learn how 401 unauthorized errors occur.

HTTP
What is HTTP 401 Error and How to Fix it

Guide to PHP 8.4 new DOM Selector Feature

Learn about PHP 8.4’s new DOM Selector feature. Simplify DOM manipulation using intuitive CSS selectors for cleaner, more efficient code.

PHP
Guide to PHP 8.4 new DOM Selector Feature

Comprehensive Guide to OkHttp for Java and Kotlin

Learn how to simplify network communication in Java and Android applications using OkHttp.

HTTP
TOOLS
Comprehensive Guide to OkHttp for Java and Kotlin