Code and command line
PHP proxy setup
Most PHP HTTP code ends up in libcurl, either directly through the cURL extension or underneath Guzzle. That is good news for proxies: libcurl has mature options for the proxy address, the login, and the proxy type, and they behave the same on shared hosting, in a container or on a laptop.
This page covers the plain extension first, because it shows every option by name, then the same setup in Guzzle and a parallel version with curl_multi for scripts that fetch many pages at once.
What do you need before adding a proxy in PHP?
Tick these off before the first step. Your login and password appear in your account once the first top-up of $40 or more has been credited.
| Requirement | Detail |
|---|---|
| PHP 8.1 or newer | Older versions work, but the examples use current syntax. |
| The curl extension | Run php -m and look for curl in the list. |
| Guzzle, optional | composer require guzzlehttp/guzzle for the Guzzle step. |
| TopProxy login and password | Login such as tp7k2m9_country-us. Store the password outside your web root. |
Where do the proxy host, port and login go in PHP?
Work through the steps in order. Swap YOUR_PASSWORD for your proxy password and tp7k2m9 for your account name.
Point the cURL handle at the gateway
Set
CURLOPT_PROXYto the gateway andCURLOPT_PROXYUSERPWDtologin:password. Keeping the password in its own option means symbols inside it do not need URL encoding.<?php $login = 'tp7k2m9_country-us'; $password = getenv('TOPPROXY_PASSWORD') ?: 'YOUR_PASSWORD'; $ch = curl_init('https://ipinfo.io/json'); curl_setopt_array($ch, [ CURLOPT_PROXY => 'http://proxy.topproxy.shop:8080', CURLOPT_PROXYUSERPWD => $login . ':' . $password, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 30, ]); $body = curl_exec($ch); if ($body === false) { echo 'Error: ' . curl_error($ch) . PHP_EOL; } else { echo $body . PHP_EOL; } curl_close($ch);Switch to SOCKS5 with remote name lookup
Change the port to 1080 and set
CURLOPT_PROXYTYPEtoCURLPROXY_SOCKS5_HOSTNAME. The plainCURLPROXY_SOCKS5constant resolves the target on your server instead of at the exit.curl_setopt_array($ch, [ CURLOPT_PROXY => 'proxy.topproxy.shop:1080', CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5_HOSTNAME, CURLOPT_PROXYUSERPWD => $login . ':' . $password, ]);Use the same login in Guzzle
Guzzle takes a proxy URL in its
proxyrequest option. Here the login is part of the URL, so pass the password throughrawurlencode. Setting the option on the client applies it to every request.<?php use GuzzleHttp\Client; $proxy = 'http://tp7k2m9_country-us:' . rawurlencode(getenv('TOPPROXY_PASSWORD')) . '@proxy.topproxy.shop:8080'; $client = new Client(['proxy' => $proxy, 'timeout' => 30]); $res = $client->get('https://ipinfo.io/json'); echo $res->getBody() . PHP_EOL;Fetch several pages in parallel
curl_multi runs many handles at once from one PHP process. Each handle can carry its own login, so one batch can mix countries or sessions.
<?php $urls = ['https://ipinfo.io/json', 'https://ipinfo.io/json', 'https://ipinfo.io/json']; $mh = curl_multi_init(); $handles = []; foreach ($urls as $i => $url) { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_PROXY => 'http://proxy.topproxy.shop:8080', CURLOPT_PROXYUSERPWD => 'tp7k2m9_country-us:' . getenv('TOPPROXY_PASSWORD'), CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, ]); curl_multi_add_handle($mh, $ch); $handles[$i] = $ch; } do { $status = curl_multi_exec($mh, $running); if ($running) curl_multi_select($mh); } while ($running && $status === CURLM_OK); foreach ($handles as $ch) { echo curl_multi_getcontent($ch) . PHP_EOL; curl_multi_remove_handle($mh, $ch); } curl_multi_close($mh);
How can you confirm the proxy is active in PHP?
Decode the ipinfo.io response and print the address and country. If the handle returns false, curl_error names the failing stage, which usually points to the login or the port.
$info = json_decode($body, true);
echo $info['ip'] . ' ' . $info['country'] . PHP_EOL;The response shown here is shortened to the fields that matter. The region and time zone follow the exit, in this case Mexico.
{
"ip": "198.51.100.47",
"city": "Mexico City",
"region": "Mexico City",
"country": "MX",
"timezone": "America/Mexico_City"
}198.51.100.47 is an address reserved for documentation. A live check prints a real exit.
How do you switch between rotating and held IPs in PHP?
A rotating login gives a new exit for each new connection. libcurl reuses connections inside one handle, so reuse a handle when you want the same exit and create a new one when you want a change. For a guaranteed fixed address, use a session login.
CURLOPT_PROXYUSERPWD => 'tp7k2m9_country-us:' . $passwordHow the login is put together
Every setting lives in the login, joined with underscores. Nothing else in PHP changes when you switch location or session.
| Part | Meaning | Rule |
|---|---|---|
| tp7k2m9 | Your account name | Always first. Yours is shown in your account and replaces tp7k2m9 in every example here. |
| _country-us | Exit country | Two-letter code in lowercase. 64 countries to choose from. |
| _city-new-york | Exit city, optional | Lowercase, spaces become hyphens. 172 cities in total. |
| _session-a1b2c3 | Session name, optional | Letters and digits you pick. Same name, same exit. New name, new exit. |
| _life-30 | Session life in minutes | Only with a session. Residential 1 to 120 minutes, mobile 1 to 60 minutes, long session ISP up to 12 hours (life 720). |
All parts togethertp7k2m9_country-us_city-new-york_session-a1b2c3_life-30
How do you choose an exit country or city in PHP?
Location goes in the login. The country code is two lowercase letters, the city is lowercase with hyphens.
CURLOPT_PROXYUSERPWD => 'tp7k2m9_country-mx:' . $passwordCodes and city values for all 64 countries are listed on the proxy locations pages.
How do you fix a 407 or connection error in PHP?
Find the line that matches what you see. Most problems come down to the login, the port or the network you are on.
| Symptom | Likely cause | Fix |
|---|---|---|
Received HTTP code 407 from proxy after CONNECT | Login or password refused. | Check the login and that the password environment variable is set for the PHP process (web servers often have a different environment). |
Failed to connect to proxy.topproxy.shop port 8080 | Your host blocks outbound connections on that port. | Ask the hosting provider to allow it, or test the SOCKS5 port. |
| Guzzle ignores the proxy | An array form of the proxy option set only one scheme. | Pass a single string, or set both http and https keys. |
Can't complete SOCKS5 connection | The HTTP proxy type was used on port 1080, or the reverse. | Pair port 1080 with CURLPROXY_SOCKS5_HOSTNAME, and port 8080 with the default HTTP type. |
Still stuck? Run the same login through the cURL check. If cURL connects, the issue is in the PHP settings. If it does not, write to support with the error text.
What kind of proxy pairs well with PHP?
All products sit behind the same gateway and share one prepaid balance, so moving from one to another means buying the other product and changing the login, nothing more.
- Rotating Residential ProxiesHousehold exits for price checks and content fetches from PHP jobs.$0.23 per GB
- Datacenter Proxy PoolVery low cost per IP for cron tasks that hit simple endpoints.$0.58 per 100 IPs per month
- Static Residential IPsA fixed address that a partner service can allowlist for your server.$2.34 per IP per month
Calling pages from code at scale? The TopProxy API docs cover the Scraping API, which renders pages and retries on its own.
What else do PHP users ask about proxies?
Does CURLOPT_HTTPPROXYTUNNEL need to be set?
No. For https URLs libcurl opens a CONNECT tunnel through an HTTP proxy on its own. The option is only needed if you want to tunnel plain http URLs as well.
Can I keep the password out of my code?
Yes. Read it with getenv from the environment of the PHP process, or from a config file outside the public directory. Avoid hard-coding it in files that are deployed with the site.
Does file_get_contents work with a proxy?
It can, through a stream context with the proxy option and a Proxy-Authorization header, but https tunnelling through stream contexts is limited. The cURL extension is the dependable route.
How do I pick a different country per request in Guzzle?
Pass the proxy option on the request instead of on the client, with a login that names the country you want for that call.
Is there a limit on parallel handles?
There is no per-login thread limit on rotating products. Keep the number of parallel handles at a level the target site accepts, and add short pauses if you see 429 answers.
Related setup guides
- cURLOne terminal command to prove your login works, then SOCKS5 with remote DNS and a reusable config file.
- Python RequestsA proxies dictionary, a Session with retries, SOCKS5 through PySocks and a pool of sticky logins.
- Gohttp.Transport with http.ProxyURL and url.UserPassword, SOCKS5 through the same field, and goroutine-safe clients.
- Javajava.net.http.HttpClient with a ProxySelector and an Authenticator, the Basic tunnelling switch, and OkHttp.
Ready to connect PHP?
Open an account, add $40 or more in crypto, and your login and password are waiting in the account area.