By Leo Lin, Product Manager at FlashIP (https://www.flaship.com/), Singapore. Disclosure: I work on FlashIP.
PHP projects usually hit a proxy in three places: Composer downloading packages, raw curl_* calls, and Guzzle. All three end up in libcurl, but each reads proxy settings a little differently. Here's what the official docs say, plus what I saw testing on the box with PHP 8.4, libcurl 8.14, Composer 2.11 and Guzzle 7, pointing everything at a dead proxy (127.0.0.1:9) so a failure proves the proxy was used. Hostnames and credentials below are placeholders.
1. Composer: environment variables only
Composer has no proxy setting in composer.json or config.json. Running composer config -g http-proxy ... fails with "Setting http-proxy does not exist". Use environment variables instead:
export http_proxy=http://user:pass@proxy.example.com:8080
export https_proxy=http://user:pass@proxy.example.com:8080
export no_proxy=localhost,127.0.0.1,git.internal.example.com
composer install
From Composer's "behind a proxy" FAQ:
- Lowercase names are preferred, but uppercase ones are accepted. In my test,
https_proxyandHTTPS_PROXYboth sent Packagist traffic to the dead proxy (curl error 7 while downloading https://repo.packagist.org/packages.json). -
Set both. Packagist is HTTPS, so
http_proxyalone did nothing.composer show -a psr/logstill worked directly. -
Credentials go in the URL. Percent-encode reserved characters in the user name and password separately (
p@ssbecomesp%40ss). -
no_proxyis a comma-separated list.example.comcovers the domain and its subdomains, a leading dot is ignored, and ports, IPs, CIDR blocks and*are allowed. Bothno_proxy=packagist.organdno_proxy=.packagist.orglet Composer reachrepo.packagist.orgdirectly. -
Non-CLI use (Composer embedded in a CMS, for example) ignores
http_proxy. UseCGI_HTTP_PROXYfor HTTP there. This guards against "httpoxy", where a request header becomes anHTTP_PROXYvariable. -
HTTP_PROXY_REQUEST_FULLURIandHTTPS_PROXY_REQUEST_FULLURIare deprecated and no longer used.
Two things are worth knowing. When a proxy is set, composer diagnose adds a "Checking HTTP proxy" step, which is the fastest way to see what Composer picked up. And if your corporate proxy re-signs TLS, composer config -g cafile /path/to/corp-ca.pem is the relevant config key (openssl.cafile in php.ini also works).
2. PHP's cURL extension
With no options set, libcurl reads the environment itself, so curl_init() inherits a proxy you might not expect:
-
https_proxyorHTTPS_PROXYsenthttps://example.com/to the dead proxy. - For
http://URLs, only lowercasehttp_proxyis read. UppercaseHTTP_PROXYwas ignored in my test, for the same httpoxy reason. -
no_proxyis respected.
To set the proxy explicitly:
$ch = curl_init('https://api.example.com/');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_PROXY => 'http://proxy.example.com:8080',
CURLOPT_PROXYUSERPWD => 'user:pass',
CURLOPT_NOPROXY => 'localhost,127.0.0.1,.internal.example.com',
]);
$body = curl_exec($ch);
-
CURLOPT_PROXYtakes a host or URL. Asocks5h://scheme makes it a SOCKS5 proxy with DNS resolved by the proxy. You can also setCURLOPT_PROXYTYPE. -
CURLOPT_PROXYUSERPWDtakesuser:pass. -
CURLOPT_NOPROXYis a comma-separated list where each name matches itself and its subdomains.*disables the proxy for everything. WithCURLOPT_NOPROXY => '.example.com', a request toexample.comwent direct. - Setting
CURLOPT_PROXYto an empty string turns off the proxy, including the one from the environment.
3. Guzzle: env defaults and the proxy option
When a GuzzleHttp\Client is constructed, it reads HTTPS_PROXY, NO_PROXY and (only under the CLI SAPI) HTTP_PROXY into its default proxy option. Per-request, you pass a string or an array:
$client = new GuzzleHttp\Client();
$client->request('GET', 'https://api.example.com/', [
'proxy' => [
'http' => 'http://user:pass@proxy.example.com:8080',
'https' => 'http://user:pass@proxy.example.com:8080',
'no' => ['localhost', 'internal.example.com'],
],
]);
Here's what I found:
-
A string applies to every scheme. An array uses the key matching the URL scheme (
httporhttps). -
Your
proxyoption replaces the env defaults, includingno. The Guzzle docs say that when you passproxy, you have to supplynoyourself, for exampleexplode(',', getenv('NO_PROXY')). -
A missing key doesn't mean "direct". With
['http' => ...]only, Guzzle sets no proxy for anhttps://request, so libcurl falls back tohttps_proxyfrom the environment. My test with that array plushttps_proxyset still hit the dead proxy. To force direct, use'proxy' => '', which worked in my test. -
Leading dots matter in
no. Guzzle's matcher treatsexample.comas the host plus its subdomains, but.example.commatches subdomains only.NO_PROXY=.example.comstill senthttps://example.com/through the proxy. Composer and libcurl ignore the dot. There's also no CIDR support in Guzzle's list. -
SOCKS works through the cURL handler.
'proxy' => 'socks5h://host:1080'was handed to libcurl as-is.
Quick checklist
- Set both
http_proxyandhttps_proxyfor Composer, lowercase if possible. - Remember that raw
curl_*calls inherithttps_proxysilently. SetCURLOPT_PROXYexplicitly, or set it to''to opt out. - In Guzzle, pass
nowhenever you passproxy, and write entries without a leading dot. - Use
composer diagnoseand a dead-port proxy to prove which path is in use before debugging a real one.
If you need an external proxy to test against, FlashIP (https://www.flaship.com/) offers residential, mobile 4G/5G, static ISP and datacenter proxies over HTTP(S) and SOCKS5. The settings above work the same for any HTTP or SOCKS5 endpoint.
Sources
- Composer, How to use Composer behind a proxy: https://getcomposer.org/doc/faqs/how-to-use-composer-behind-a-proxy.md
- Composer config (cafile, capath): https://getcomposer.org/doc/06-config.md
- PHP manual, cURL constants: https://www.php.net/manual/en/curl.constants.php
- Guzzle request options (proxy): https://docs.guzzlephp.org/en/stable/request-options.html#proxy
- Guzzle quickstart (environment variables): https://docs.guzzlephp.org/en/stable/quickstart.html#environment-variables
- httpoxy: https://httpoxy.org/
Top comments (0)