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.

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(' world.proxymesh.com', 31280);
$proxy1->setAuth('PROXY-USER', 'xxxxxx');
$proxy1 = new HttpProxy(' us.proxymesh.com', 31280);
$proxy1->setAuth(' PROXY-USER ', 'xxxxxx');
$proxy2->setSocket($proxy1);
$proxy1->setSocket($socket);
$webResponse = $proxy1->sendRequest($request);

Still need help? Contact Us Contact Us