How do I get the HTML code of a web page in PHP?

How do I get the HTML code of a web page in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How do I get the HTML code of a web page in PHP?

Retrieving the full HTML source of an external URL is a common requirement for tasks like RSS feed parsing, content aggregation, or scraping structured data. In PHP, you cannot simply open a link from within your browser; you need to handle the network request programmatically and parse the response body as text.

The most robust approach involves using cURL, which provides low-level control over HTTP transactions. While simpler alternatives exist, cURL handles headers, redirects, and SSL certificates effectively without requiring additional dependencies.

To begin, you must ensure the curl extension is enabled in your PHP environment, which is standard on almost all modern installations. Once confirmed, you initialize a session with curl_init() and set specific options to mimic a legitimate browser request. If you skip these steps, many websites will block your request or return an incomplete document due to missing headers like User-Agent or Accept.

Here is a minimal implementation that fetches the HTML from a target URL:

$url = 'https://stackoverflow.com/questions/ask';

$ch = curl_init($url);

// Set options required for reliable fetching
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as string instead of outputting directly
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Automatically follow redirects (301/302)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Verify SSL certificates for security
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; PHP-Scraper/1.0)'); // Simulate a browser

// Execute the request and capture the response
$html = curl_exec($ch);

if ($html === false) {
    // Handle errors gracefully
    $error = curl_error($ch);
    die("cURL error: $error");
} else {
    // Store in variable for further processing
    echo $html; 
}

curl_close($ch);

This code returns the raw HTML string stored in $html. You can then pass this variable to a DOM parser like DOMDocument or SimpleXML to extract specific elements. For instance, you might want to pull the title tag or scrape a list of comments from the fetched content.

A critical detail often overlooked is handling cookies and session tokens. Many modern sites, such as Stack Overflow, require authentication or rely on CSRF tokens embedded in the HTML. If you attempt to fetch such a page without an active session cookie, the server may return a login page instead of the actual content you are targeting. In these scenarios, you must either pre-fetch the login page to capture the necessary cookies or use tools like curl_setopt($ch, CURLOPT_COOKIEJAR) and CURLOPT_COOKIEFILE if managing persistent sessions across multiple requests.

Another frequent pitfall is dealing with JavaScript-rendered content. The HTML returned by cURL represents the initial state of the page before the browser executes JavaScript. If the data you need only exists after an AJAX call or dynamic DOM manipulation (e.g., a list that loads when scrolling), static fetching will fail. In such cases, cURL alone is insufficient; you would need to use a headless browser automation tool like Puppeteer or Selenium, which run inside your application to render the JavaScript first.

When dealing with large documents or high-frequency scraping, memory management becomes a factor. Reading the entire response into a single string works fine for standard articles but can exhaust memory on massive dashboards. If performance is a concern, consider streaming the output directly to a file or processing it in chunks using CURLOPT_WRITEFUNCTION callbacks.

Finally, always respect the target site's robots.txt file and terms of service. Aggressive scraping can trigger rate limiting or IP bans. Implementing exponential backoff delays between requests is a best practice to ensure your script remains polite and sustainable. For enterprise-level web interactions where you control both the client and server logic, Laravel provides elegant wrappers for HTTP requests via its Http facade, which abstracts much of this complexity while maintaining full control over headers and sessions.

Our network