PHP Configuration for HTTP Basic Proxy Authentication

This article describes how to configure the Proxy Host with Basic Authentication in the PHP scripting language for an HTTP client.

ProxyMesh also supports IP address authentication, for which you do not need to use a username and password. Instead, set the http_proxy environment variable to http://PROXYHOST:PORT.

For PHP configuration, you can use the included Client URL Library (cURL). To configure proxy settings, use the curl_setopt function to set CURLOPT_PROXY to the proxy host and port, such as PROXYHOST:PORT. Optionally, for authentication, you can set CURLOPT_PROXYUSERPWD to USERNAME:PASSWORD.

X-ProxyMesh-IP with HTTPS Requests

HTTPS requests with custom headers – such as X-ProxyMesh-IP to specify an IP address – are difficult because the actual request headers are encrypted. With PHP, however, you can use CURLOPT_PROXYHEADER to send the X-ProxyMesh-IP header in PHP cURL. Use the following option:

'X-ProxyMesh-IP: nn.nn.nn.nn:nnnn'

In this way, you should be able to request the same IP address at any time, assuming continued availability.

Chaining Proxies in PHP

Some queries are blocked by server security tools that apply learned blocking information automatically on all servers enabled for them. This can often be an issue for requests through the world proxy. However, it's important to avoid or bypass blocks if your use cases include avoidance of rate limits, high-traffic operations, high anonymity, or screen scraping.

If you are on a shared hosting service, installing VPN is not an available workaround for blocking. The X-ProxyMesh-Country header can be a useful workaround unless you are connecting to an HTTPS site, in which case you need special handling to pass the header in. Details are provided in Proxy Server Headers over HTTPS.

As a more consistently effective workaround, consider proxy chaining. With this approach, you connect to proxies in sequential steps until you reach your intended destination.

Below is a sample of code for a PHP connector client based on web sockets. Please also see the proxy_chain.php example in the PHP WebSocket2 Library.

Please Note: The code sample below is for the HTTP protocol only. At this time we do not support the SOCKS protocol.

$proxy1 = new HttpProxy('', 31280);
$proxy1->setAuth('PROXY-USER', 'xxxxxx');
$proxy1 = new HttpProxy('', 31280);
$proxy1->setAuth(' PROXY-USER ', 'xxxxxx');
$webResponse = $proxy1->sendRequest($request);

Still need help? Contact Us Contact Us