Originally published on proxymint.
PHP cURL proxy setup that works: CURLOPT_PROXY and PROXYUSERPWD, special characters, SOCKS5 and socks5h, env variables, reuse, rotation and the 407 errors.
TL;DR
- Two options do the setup. Set CURLOPT_PROXY to http://HOST:PORT and CURLOPT_PROXYUSERPWD to USERNAME:PASSWORD. HTTPS URLs then go through a CONNECT tunnel without any other option.
- CURLOPT_USERPWD is the wrong option. It logs in to the website, not the proxy, so the proxy answers 407 even with the right password.
- Special characters need care. In the proxy URL, percent-encode the login with rawurlencode(). CURLOPT_PROXYUSERPWD takes it raw, but libcurl decodes %XX sequences there too.
- Use socks5h:// for SOCKS5. With socks5:// PHP resolves the website's name on its own server; with socks5h:// the proxy does the lookup.
Key takeaways
- Always write the proxy port. libcurl 8.5.0 dialled port 1080 for a proxy address without one, http:// included.
- Check CURLINFO_HTTP_CODE on every response. A rejected login on an http:// URL comes back as the proxy's 407 page with no cURL error; CURLOPT_FAILONERROR turns it into errno 22.
- Set the proxy on the handle, not in the environment. libcurl ignores uppercase HTTP_PROXY, a no_proxy variable overrides CURLOPT_PROXY, and PHP-FPM clears the environment by default.
- One handle keeps one tunnel and one exit. Use CURLOPT_FRESH_CONNECT or a new handle per request when every request should be able to get a new IP.
Read the full guide on proxymint, with the commands, tables and sources.
Top comments (0)