ProxyContext
final class ProxyContext (View source)
Resolves HTTP(S)_PROXY / NO_PROXY environment variables into stream-context options usable by PHP's HTTP wrapper. PHP's stream wrapper does not pick up these variables on its own, so each call site needs to opt in explicitly.
Methods
Build the proxy-related http context options for a target URL, or an empty array when no proxy applies.
Decide whether a target URL should be sent through the proxy. Honors the same env vars and NO_PROXY rules (including CIDR and host:port entries) as httpOptionsFor(), so callers that build a Guzzle/curl proxy config can skip configuring it when the target is bypassed.
Build a Guzzle-shaped proxy config from the environment, suitable for
passing as the proxy option to a Guzzle client (or any consumer that
accepts Guzzle's request options, e.g. league/oauth2-client).
Merge proxy options into an existing stream-context options array under
the http sub-key. Returns the array unchanged when no proxy applies.
Details
static array
httpOptionsFor(string $url)
Build the proxy-related http context options for a target URL, or an empty array when no proxy applies.
static bool
shouldProxy(string $url)
Decide whether a target URL should be sent through the proxy. Honors the same env vars and NO_PROXY rules (including CIDR and host:port entries) as httpOptionsFor(), so callers that build a Guzzle/curl proxy config can skip configuring it when the target is bypassed.
static array|null
guzzleProxyConfig()
Build a Guzzle-shaped proxy config from the environment, suitable for
passing as the proxy option to a Guzzle client (or any consumer that
accepts Guzzle's request options, e.g. league/oauth2-client).
Returns null when no proxy is configured.
Note: Guzzle's own NO_PROXY matcher is suffix-only - CIDR and host:port entries in NO_PROXY are not understood when Guzzle decides per-request whether to bypass. Use shouldProxy() to make that decision in PHP and skip building a config for bypassed targets.
static array
applyTo(array $options, string $url)
Merge proxy options into an existing stream-context options array under
the http sub-key. Returns the array unchanged when no proxy applies.
The Proxy-Authorization header is appended to any existing header
value rather than overwriting it, so call-site headers (X-API-Key,
Authorization, etc.) are preserved.