# Use a proxy with PHP cURL

Complete proxy requests using PHP cURL extensions, accounting environment variables, and connection timeouts.

PHP services can use cURL to extend the configuration of proxy hosts, ports, and authentication, without having to stack credentials into the target URL.

## Precondition

- The PHP runtime has enabled the cURL extension.
- The account is closed. `us.zzhttp.com:7878`; Injection through controlled environment `PROXY_USERNAME` and `PROXY_PASSWORD`.
- The production environment shuts down the detailed debug output containing sensitive request information.

## Complete example

```php
<?php

$handle = curl_init('https://ipinfo.io/json');
curl_setopt_array($handle, [
    CURLOPT_PROXY => 'us.zzhttp.com',
    CURLOPT_PROXYPORT => 7878,
    CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
    CURLOPT_PROXYUSERPWD => sprintf(
        '%s:%s',
        getenv('PROXY_USERNAME'),
        getenv('PROXY_PASSWORD')
    ),
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_RETURNTRANSFER => true,
]);

$body = curl_exec($handle);
if ($body === false) {
    $message = curl_error($handle);
    curl_close($handle);
    throw new RuntimeException($message);
}

$status = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
if ($status >= 400) {
    throw new RuntimeException("HTTP {$status}");
}

echo $body;
```

## Expected results

- cURL uses an HTTP proxy to access the HTTPS target and return the export information.
- The connection waits for up to 10 seconds and the entire request waits for up to 30 seconds.
- The business code checks for transmission errors and HTTP status at the same time.

## Error handling

- 407 Priority Check `CURLOPT_PROXYUSERPWD` The corresponding accounts are complete.
- Connection timeout first checks the proxy address, port, and server exit network.
- TLS errors should not be masked by turning off certificate validation.

## Safety tips

Do not enable public debug logs that record authentication headers. The proxy URL, proxy username, and password are removed before exception reporting, leaving only the cURL error code and desensitized context.

html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}
