Introduction: Why PHP is an Ideal Tool for Parsing
\n\nWhen 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\nOn 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\nIf you have ever wanted to automatically collect competitor prices, news, or contacts — you have come to the right place. Let's go!
\n\nParsing and Scraping: What's the Difference?
\n\nBefore 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\nIn 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
Additionally, you can use libraries (we will discuss them later), but built-in functions are sufficient to start.
\n\nInstallation and Environment Setup
\n\nTo work, you will need PHP version 7.4 or higher with the curl and dom extensions. You can check their availability like this:
php -m | grep -E "curl|dom"\n\nIf the output is empty, install them via the package manager:
\n\nsudo apt install php-curl php-xml\n\nEverything is ready. Now let's create our first script for parsing a website.
\n\nExample 1: Simple Parsing with cURL and DOMDocument
\n\nLet'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\nNote: 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.
Example 2: Working with Forms and POST Requests
\n\nOften, 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,