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_proxy and HTTPS_PROXY both 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_proxy alone did nothing. composer show -a psr/log still worked directly.
  • Credentials go in the URL. Percent-encode reserved characters in the user name and password separately (p@ss becomes p%40ss).
  • no_proxy is a comma-separated list. example.com covers the domain and its subdomains, a leading dot is ignored, and ports, IPs, CIDR blocks and * are allowed. Both no_proxy=packagist.org and no_proxy=.packagist.org let Composer reach repo.packagist.org directly.
  • Non-CLI use (Composer embedded in a CMS, for example) ignores http_proxy. Use CGI_HTTP_PROXY for HTTP there. This guards against "httpoxy", where a request header becomes an HTTP_PROXY variable.
  • HTTP_PROXY_REQUEST_FULLURI and HTTPS_PROXY_REQUEST_FULLURI are 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_proxy or HTTPS_PROXY sent https://example.com/ to the dead proxy.
  • For http:// URLs, only lowercase http_proxy is read. Uppercase HTTP_PROXY was ignored in my test, for the same httpoxy reason.
  • no_proxy is 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_PROXY takes a host or URL. A socks5h:// scheme makes it a SOCKS5 proxy with DNS resolved by the proxy. You can also set CURLOPT_PROXYTYPE.
  • CURLOPT_PROXYUSERPWD takes user:pass.
  • CURLOPT_NOPROXY is a comma-separated list where each name matches itself and its subdomains. * disables the proxy for everything. With CURLOPT_NOPROXY => '.example.com', a request to example.com went direct.
  • Setting CURLOPT_PROXY to 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 (http or https).
  • Your proxy option replaces the env defaults, including no. The Guzzle docs say that when you pass proxy, you have to supply no yourself, for example explode(',', getenv('NO_PROXY')).
  • A missing key doesn't mean "direct". With ['http' => ...] only, Guzzle sets no proxy for an https:// request, so libcurl falls back to https_proxy from the environment. My test with that array plus https_proxy set still hit the dead proxy. To force direct, use 'proxy' => '', which worked in my test.
  • Leading dots matter in no. Guzzle's matcher treats example.com as the host plus its subdomains, but .example.com matches subdomains only. NO_PROXY=.example.com still sent https://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

  1. Set both http_proxy and https_proxy for Composer, lowercase if possible.
  2. Remember that raw curl_* calls inherit https_proxy silently. Set CURLOPT_PROXY explicitly, or set it to '' to opt out.
  3. In Guzzle, pass no whenever you pass proxy, and write entries without a leading dot.
  4. Use composer diagnose and 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