Parsing and Scraping Data in PHP: From Simple to Professional

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why PHP is an Ideal Tool for Parsing

\n\n

When we talk about website parsing, many immediately think of Python with its BeautifulSoup or Scrapy. But let's be honest: PHP lives on every second server, and if your project is already on PHP — dragging an entire Python stack for just one parsing task is simply irrational. PHP handles data collection perfectly, especially when you need to quickly process HTML or JSON, access an API, or extract content from a dozen pages.

\n\n

On the pythonlib.ru website, we are used to talking about Python, but today we will make an exception. Because PHP is not just a language for "business card websites." It is a powerful tool for web scraping that can work with cURL, DOMDocument, XPath, and even simulate browser behavior. In this article, we will cover, using real examples, how to parse websites in PHP, avoid blocks, and handle dynamic content.

\n\n

If you have ever wanted to automatically collect competitor prices, news, or contacts — you have come to the right place. Let's go!

\n\n

Parsing and Scraping: What's the Difference?

\n\n

Before writing code, let's clarify the terms. Data parsing in PHP is the process of extracting structured information from HTML, XML, or JSON. Data scraping in PHP is a broader concept that includes loading a page, bypassing protections, and extracting data. Simply put: parsing is "reading," while scraping is "mining."

\n\n

In PHP for scraping, we will need two main tools:

\n
    \n
  • cURL — for sending HTTP requests and receiving responses from the server
  • \n
  • DOMDocument or SimpleXML — for parsing the received HTML/XML
  • \n
\n\n

Additionally, you can use libraries (we will discuss them later), but built-in functions are sufficient to start.

\n\n

Installation and Environment Setup

\n\n

To work, you will need PHP version 7.4 or higher with the curl and dom extensions. You can check their availability like this:

\n\n
php -m | grep -E "curl|dom"
\n\n

If the output is empty, install them via the package manager:

\n\n
sudo apt install php-curl php-xml
\n\n

Everything is ready. Now let's create our first script for parsing a website.

\n\n

Example 1: Simple Parsing with cURL and DOMDocument

\n\n

Let's say we want to collect article titles from a blog. Let's write simple but working code:

\n\n
<?php\n// URL for parsing\n$url = 'https://example-blog.com';\n\n// Initialize cURL\n$ch = curl_init($url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);\ncurl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');\n\n$html = curl_exec($ch);\ncurl_close($ch);\n\n// Create DOMDocument and load HTML\n$dom = new DOMDocument();\nlibxml_use_internal_errors(true); // Ignore HTML parsing errors\n$dom->loadHTML($html);\nlibxml_clear_errors();\n\n// Use XPath to find h2 titles\n$xpath = new DOMXPath($dom);\n$titles = $xpath->query('//h2');\n\nforeach ($titles as $title) {\n    echo $title->nodeValue . "\\n";\n}\n?>
\n\n

Note: we set a User-Agent — this is minimal protection against blocking. Without it, sites often return a 403 error. We also use CURLOPT_FOLLOWLOCATION for automatic redirect following.

\n\n

Example 2: Working with Forms and POST Requests

\n\n

Often, data is hidden behind a search form or authentication. Parsing in PHP allows you to emulate form submission:

\n\n
<?php\n$url = 'https://example.com/search';\n$postData = http_build_query([\n    'query' => 'PHP scraping',\n    'submit' => 'Search'\n]);\n\n$ch = curl_init($url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_POST, true);\ncurl_setopt($ch, CURLOPT_POSTFIELDS,                

Blogs

Book Recommendations