June 19th, 2023
How to Build a Super Simple HTTP proxy in PHP in just 30 lines of code?

Its super easy to create a rudimentary reverse proxy server in php.

In this tutorial, we will learn how to build a basic HTTP proxy server using PHP. A proxy server acts as an intermediary between clients and web servers, allowing clients to send requests to the proxy server, which then forwards those requests to the target web servers. This can be useful for various purposes such as caching, filtering, and anonymizing client requests. Let's get started!

Step 1: Set Up the Project First, create a new directory for your project and navigate into it. This will serve as the root directory for our proxy server.

Step 2: Create a New PHP File Next, create a new PHP file in your project directory. You can name it whatever you like. For this example, let's name it proxy.php.

Step 3: Implement the Proxy Server Open the proxy.php file in your preferred text editor and paste the following code:

<?php

// Define the target URL
$targetUrl = "<https://www.example.com>";

// Get the client request URL
$clientUrl = $_SERVER['REQUEST_URI'];

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $targetUrl . $clientUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the cURL request
$response = curl_exec($ch);

// Get the response headers
$responseHeaders = curl_getinfo($ch);

// Close the cURL session
curl_close($ch);

// Forward the response headers to the client
foreach ($responseHeaders as $headerName => $headerValue) {
    header("$headerName: $headerValue");
}

// Forward the response body to the client
echo $response;

Here's an explanation of the code with references to specific parts:

Target URL and Client Request URL:

$targetUrl = "<https://www.example.com>";
$clientUrl = $_SERVER['REQUEST_URI'];

The $targetUrl variable holds the URL of the target web server that the proxy server will forward requests to. The $clientUrl variable captures the client's request URL by accessing the REQUEST_URI value from the $_SERVER superglobal. This represents the path and query parameters of the client's request.

Initializing and Configuring cURL:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $targetUrl . $clientUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

This code sets up a cURL session by initializing it with curl_init(). Then, using curl_setopt(), we configure the cURL session with the target URL and client URL. The CURLOPT_URL option specifies the complete URL to which the request should be sent, combining the target URL and the client URL. The CURLOPT_RETURNTRANSFER option is set to true to instruct cURL to return the response instead of outputting it directly.

Executing the cURL Request:

$response = curl_exec($ch);

The curl_exec() function executes the cURL request and sends the request to the target URL. The response from the target server is stored in the $response variable.

Forwarding Response Headers:

$responseHeaders = curl_getinfo($ch);

foreach ($responseHeaders as $headerName => $headerValue) {
    header("$headerName: $headerValue");
}

The curl_getinfo() function retrieves the response headers from the cURL session and stores them in the $responseHeaders variable. We then iterate through the response headers and use the header() function to forward each header to the client. This ensures that the client receives the appropriate response headers from the target server.

Forwarding Response Body:

echo $response;

Finally, we output the response body ($response) to the client. This forwards the response content from the target server back to the client, completing the proxy server's role.

Step 4: Run the Proxy Server Save the proxy.php file. You can now run the PHP built-in web server from your project directory using the following command:

php -S localhost:9097

The proxy server is now running on http://localhost:9097.

Step 5: Test the Proxy Server To test the proxy server, open your web browser and configure it to use localhost as the proxy server and 9097 as the port. Now, when you browse any website using your browser, the requests will be routed through the proxy server.

Congratulations! You have successfully built a simple HTTP proxy server using PHP. This basic proxy server allows clients to send requests, and the server forwards those requests to the target URL specified in the code.

This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.

Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.

Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.

With millions of high speed rotating proxies located all over the world,With our automatic IP rotationWith our automatic User-Agent-String rotation (which simulates requests from different, valid web browsers and web browser versions)With our automatic CAPTCHA solving technology,

Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.

The whole thing can be accessed by a simple API like below in any programming language.

In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so.

curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"

We have a running offer of 1000 API calls completely free. Register and get your free API Key here.

Share this article:

Get our articles in your inbox

Dont miss our best tips/tricks/tutorials about Web Scraping
Only great content, we don’t share your email with third parties.
Icon