ProxyForge Download

PHP cURL Proxy Rotation: Set Up and Rotate Proxies for Web Scraping

Introduction

Web scraping can be an effective way to gather data from websites at scale. However, many websites have anti-scraping measures that block requests coming from a single IP address or detect patterns indicative of automated traffic. To bypass these restrictions, proxy rotation is often employed. This guide will walk you through setting up and rotating proxies using PHP cURL for web scraping tasks.

Setting Up Basic Proxy Rotation

To use a proxy with cURL in PHP, you need to set the CURLOPT_PROXY option. If your proxy requires authentication, you can also specify the username and password using CURLOPT_PROXYUSERPWD.

Example: Using HTTP Proxies

Example: Using SOCKS5 Proxies

If your proxies are of the SOCKS5 type, you need to specify CURLOPT_PROXYTYPE as well.

Implementing Basic Authentication with Proxies

If your proxies require authentication, you can pass the username and password using CURLOPT_PROXYUSERPWD.

Handling Free Proxy Failures with Professional Services

Free proxies can be unreliable, and you might frequently encounter issues like timeouts, blocked IPs, or unresponsive servers. To ensure high availability and performance for your web scraping tasks, consider using professional proxy services such as Bright Data and SmartProxy.

Example: Using Bright Data's Gateway Format in PHP

Bright Data offers a gateway that simplifies proxy usage by providing a single endpoint to handle multiple proxies. This can be beneficial for managing large-scale operations.

Example: Using SmartProxy's Gateway Format in PHP

Similarly, SmartProxy provides a gateway service that can be used to handle proxy requests.

Performing Parallel Requests with cURL Multi Exec

To increase the efficiency of your web scraping operations, you can perform multiple requests in parallel using curl_multi_exec.

 $proxy) {
    $ch[$index] = curl_init();
    curl_setopt($ch[$index], CURLOPT_URL, "https://example.com");
    curl_setopt($ch[$index], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch[$index], CURLOPT_PROXY, $proxy);
    
    curl_multi_add_handle($mh, $ch[$index]);
}

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

foreach ($proxies as $index => $proxy) {
    $response = curl_multi_getcontent($ch[$index]);
    echo "Response from proxy $proxy:\n$response\n";
    curl_multi_remove_handle($mh, $ch[$index]);
    curl_close($ch[$index]);
}

curl_multi_close($mh);
?>

Conclusion

Proxy rotation is a crucial technique for effective web scraping. In this guide, we covered how to set up and rotate proxies using PHP cURL, including handling HTTP, SOCKS5 proxies, and implementing basic authentication. We also discussed the benefits of professional proxy services like Bright Data and SmartProxy when dealing with free proxy limitations.

If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription: https://proxyforge.dev/#download

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 →