Building a Rotating IP and User-Agent Web Scraping Script in PHP

Building a Rotating IP and User-Agent Web Scraping Script in PHP

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

Rotating User-Agent

“The User-Agent [request header_]( is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting [user agent_](” ― [MDN web docs_](

To reach this goal, we are going to randomly select a valid User-Agent from a file containing a list of valid User-Agent strings.

Firstly, we need to get such a file. Secondly, we have to read it and extract a random line. This can be achieved with the following function:


    function getRandomUserAgent() {
      // default User-Agent
      $userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0";

      // reading a randomly chosen User-Agent string from the User-Agent list file
      if ($file = fopen("user_agents.txt", "r")) {
        $userAgents = array();

        while (!feof($file)) {
          $userAgents[] = fgets($file);

        $userAgent = $userAgents[array_rand($userAgents)];

      return trim($userAgent);


Rotating the Exit IP

To implement the IP rotation, we are going to use a proxy server.

“A proxy server is basically another computer which serves as a hub through which internet requests are processed. By connecting through one of these servers, your computer sends your requests to the server which then processes your request and returns what you were wanting. Moreover, in this way it serves as an intermediary between your home machine and the rest of the computers on the internet.” ―What Is My IP?

When using a proxy, the website we are making the request to sees the IP address of the proxy server — not ours. This enables us to scrape the target website anonymously without the risk of being banned or blocked.

Using a single proxy means that the IP server can be banned, interrupting our script. To avoid this, we would need to build a pool of proxies to route our requests through. Instead, we are going to use the Tor proxy. If you are not familiar with Tor, reading the following article is greatly recommended: How Does Tor Really Work?

programming cybersecurity startup php web-scraping

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[]( ""), we at Data EximIT have...

How This Cybersecurity Startup Is Using Machine Learning

How This Cybersecurity Startup Is Using Machine Learning. In an attempt to revolutionise the cyber threat visibility, CYFIRMA, a cyber analytics startup help in understanding the threat landscape.

PHP Tutorial - Web Scraping using Goutte

When you talk about web scraping, PHP is the last thing most people think about. In this PHP tutorial, you'll see Web Scraping using Goutte

What's the Link Between Web Automation and Web Proxies?

Web automation and web scraping are quite popular among people out there. That’s mainly because people tend to use web scraping and other similar automation technologies to grab information they want from the internet. The internet can be considered as one of the biggest sources of information. If we can use that wisely, we will be able to scrape lots of important facts. However, it is important for us to use appropriate methodologies to get the most out of web scraping. That’s where proxies come into play.

8 Reasons Why PHP and .NET Rule the World of Web Development

Building a website is the first step in the path of a successful business. For web development, PHP and .Net are the foremost languages. If any company wants to succeed in the market, it needs to strengthen its digital presence. Though social media provides a place to gain some attention, it is impossible to create a brand without an official web page. This is why businesses hire firms offering web development services to get their website designed, which would enable them to get as much traction as possible.