PHP Proxy

Sheeraz Gul Feb 08, 2022
  1. Enable the cURL Library in your PHP
  2. Create Proxy Using cURL with PHP
PHP Proxy

Proxy in PHP is created through the cURL library. cURL means client URL is one of the most powerful extensions in php.

Denial Stenberg created the cURL library to communicate between different servers.

cURL has functions that can send the request through different IPs and ports. cUrl allows us to send and receive data through URLs.

This tutorial demonstrates how to enable cURL in your PHP and create a proxy using cURL.

Enable the cURL Library in your PHP

We need to enable the cURL library before starting to create the proxy. cURL library is already present in PHP, and we have to enable it.

First of all, we need to check if cUrl is enabled or not. Create an info.php file and run it.

<?php
    phpinfo();
?>

When you run this file, it will show all the information of PHP. It is necessary to set the PATH variable for PHP before going to the next step.

If it is already set, you can proceed. Search for cURL on the info.php.

If it is not enabled, go to the main PHP folder and find the php.ini file. Most probably, the path is C:\php74\php.ini.

Open the php.ini file with a text editor. Search for curl, and you will find something like this:

;extension=php_curl.dll

Or for newer PHP versions:

;extension=curl

Now remove the ; before the extension. Save the php.ini file and exit, restart the server or localhost you are using.

extension=curl

Run info.php again and search for cURL support. You will see:

cURL support: enabled

That means cURL is enabled you are good to go. If the cURL is still not enabled, try the absolute path to php_curl.dll after extension.

extension=C:/php74/ext/php_curl.dll

Create Proxy Using cURL with PHP

cURL has many different keywords for different operations. Let’s see an example for creating a proxy request for a given URL:

//URL you want to apply cURL proxy.
$site = 'http://google.com';

// The Proxy IP address
$proxy_ip = '138.117.84.240';

//The proxy port.
$proxy_port = '999';

//Authentication information for proxy, username and password.
$Username = 'demouser';
$Password = 'demopassword';

//Initiate cURL response
$curl_reponse = curl_init($site);
curl_setopt($curl_reponse, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_reponse, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl_reponse, CURLOPT_HTTPPROXYTUNNEL , 1);

//Setting the IP address for proxy.
curl_setopt($curl_reponse, CURLOPT_PROXY, $proxy_ip);

//Setting the port for proxy.
curl_setopt($curl_reponse, CURLOPT_PROXYPORT, $proxy_port);

//Specifying the authentication information.
curl_setopt($curl_reponse, CURLOPT_PROXYUSERPWD, "$Username:$Password");

//Define the proxy server type, it is not neccessary, incase there is proxy connection aborted error. 
//The defaults type is CURLPROXY_HTTP, and the other options are CURLPROXY_SOCKS4, CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5,and CURLPROXY_SOCKS5_HOSTNAME.
curl_setopt($curl_reponse, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

//Execute the proxy request.
$output = curl_exec($curl_reponse);

//All the errors with number are given at this link from PHP Manual https://www.php.net/manual/en/function.curl-errno.php
//Error Number
echo curl_errno($curl_reponse).'<br/>';
// Error Info
echo curl_error($curl_reponse).'<br/>';

//Show the output, Return a cURL handle on success, and FALSE if there is an error.
echo $output;

The code above is the setup for creating a proxy request to run google.com. The code uses authentication for proxy.

A proxy doesn’t need to connect to the given IP and Port. If the proxy connection is successful, it will return true or a cURL handle.

If there is any error, it will print the error number and info and return false. See output for one of the most common errors:

28
Failed to connect to 138.117.84.240 port 999: Timed out

This error is mostly because of the IP and Port.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook