ProxyForge Download

PHP cURL Proxy Setup for Web Scraping: Rotation, Auth, and Server-Side Scrapers

PHP cURL Proxy Setup for Web Scraping: Rotation, Auth, and Server-Side Scrapers

Web scraping is a powerful tool for gathering data from websites. However, it can quickly lead to IP bans if you rely on a single IP address. Proxies are essential for rotating IPs and bypassing restrictions. This guide will walk you through setting up PHP cURL with proxies for web scraping tasks, including proxy rotation, authentication, and parallel requests. We'll also discuss when to use Guzzle over raw cURL.

Introduction

Before we dive into the code, let's understand why proxies are necessary in web scraping. Proxies help distribute your traffic across multiple IP addresses, reducing the likelihood of getting banned by target websites. They can also be used for authentication if the proxy requires it.

This guide assumes you have a basic understanding of PHP and cURL. We'll cover setting up proxies with different types (HTTP and SOCKS5), handling authentication, rotating proxies, making parallel requests using curl_multi_exec, and deciding between raw cURL and Guzzle.

Setting Up Basic Proxy in PHP cURL

Let's start with the basics of setting up a proxy using PHP cURL. We'll use an HTTP proxy for this example.

Example: Using HTTP Proxy

SOCKS5 Proxy Setup

For using a SOCKS5 proxy, you need to set the CURLOPT_PROXYTYPE option.

Proxy Authentication

If your proxy requires authentication, you can use the CURLOPT_PROXYUSERPWD option.

Proxy Rotation

To rotate proxies effectively, you can maintain an array of proxy strings and cycle through them.

This code will attempt to fetch a URL using each proxy in sequence until it succeeds. If a request fails, it rotates to the next proxy and retries.

Parallel Requests with curl_multi_exec

For server-side scrapers that need to handle multiple requests simultaneously, curl_multi_exec is useful. Each cURL handle can be associated with a different proxy.

 $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Set proxy for each handle
    curl_setopt($ch, CURLOPT_PROXY, $proxies[$index % count($proxies)]);
    if (strpos($proxies[$index % count($proxies)], 'socks5') !== false) {
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
    }

    curl_multi_add_handle($mh, $ch);
    $channels[] = $ch;
}

$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running > 0);

foreach ($channels as $ch) {
    $response = curl_multi_getcontent($ch);
    if ($response === false) {
        echo 'Curl error: ' . curl_error($ch);
    } else {
        echo $response;
    }
    curl_multi_remove_handle($mh, $ch);
}

curl_multi_close($mh);

foreach ($channels as $ch) {
    curl_close($ch);
}
?>

This code sets up multiple cURL handles, each with its own proxy, and executes them in parallel.

Guzzle vs. Raw cURL

Guzzle is a PHP HTTP client that simplifies working with raw cURL. It provides cleaner syntax for making requests and handling asynchronous operations. However, for basic tasks, raw cURL might be sufficient.

Example: Using Guzzle for Asynchronous Requests

 $url) {
    $options[] = [
        'proxy' => $proxies[$index % count($proxies)],
        'timeout' => 5.0,
    ];
}

$promises = [];
foreach ($urls as $index => $url) {
    $promises[] = $client->getAsync($url, $options[$index]);
}

$results = Promise\Utils::unwrap($promises);

foreach ($results as $response) {
    echo $response->getBody();
}
?>

Guzzle's asynchronous capabilities can be more convenient when dealing with multiple requests. However, for simple tasks or performance-critical applications, raw cURL might still be preferred due to its lower overhead.

Conclusion

In this guide, we covered setting up PHP cURL with proxies for web scraping tasks, including basic proxy usage, authentication, rotation, parallel requests, and deciding between raw cURL and Guzzle. Proxies are essential for effective web scraping, especially for server-side applications like price monitors or availability checkers running in cron.

If you're looking for a reliable solution to manage and rotate proxies locally without dealing with manual lists, consider ProxyForge. It auto-tests and rotates proxies on your machine with a one-off purchase of $29 at https://proxyforge.dev/#download. This can be particularly useful if you need professional or managed proxy services like Bright Data and SmartProxy for production use.

Free proxies break. ProxyForge doesn't.

The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.

Download ProxyForge free →