PHP: Proxy Servers (File- functions)

How to perform HTTP requests trough proxy servers in PHP.

Edited: 2017-08-03 13:37

Sometimes, for whatever reason, you may need to hide your own IP address. A common way to hid your IP is to use a proxy server to process your HTTP requests. Setting up your browser to use a proxy server is easy enough, but setting up scripts and programs to use proxies can require a bit more effort. This tutorial shows you how to make your PHP scripts use a proxy when performing HTTP requests with functions such as fread and file_get_contents.

If you remember the example from the last tutorial, adding a proxy can be done as easily as demonstrated in the below example:

As in the prior Tutorials, you can also use the URL provided in this example for testing purposes. ip.php is a simple php script outputting the IP of the client. If this is different than your own IP, the proxy works.

Proxy Authentication

If the proxy that you are using requires authentication, you will simply add the proxy authorization http header, in a manner similar to what is shown in the last php tutorial. Logging in to a proxy is done by combining the username and password, only separated by a single colon ":", then encoding it using base64, and send it in the Proxy-Authorization http header like shown in below example:

Getting the HTTP response headers with cURL in PHP is not straight forward. There is no build-in way to do this, but we can still cut out the headers from the response message, if CURLOPT_HEADER is true.