php: Get html source code with cURL

php: Get html source code with cURL

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

Stefan Bogdanescu

Founder & Senior Architect

php: Get html source code with cURL

When allow_url_fopen is disabled on a webhost, relying on file_get_contents() to fetch remote resources becomes impossible. This configuration restriction is common in shared hosting environments where security policies prevent scripts from accessing the filesystem via URL wrappers. In these scenarios, you need an alternative approach to retrieve HTML source code or binary data from external domains. The most robust and widely supported solution is using the cURL extension.

Before diving into implementation, it is critical to verify that the cURL extension is actually enabled on your server. You can check this by running phpinfo() and searching for "cURL," or by executing a simple PHP script with extension_loaded('curl'). If the function returns false, you cannot proceed without enabling the extension first, which usually requires access to your php.ini file or contacting your hosting provider. Assuming cURL is available, here is how you can fetch HTML content programmatically.

The standard method involves initializing a CurlHandle and configuring it to suppress output streams. When using cURL for remote requests, the default behavior often redirects responses to the standard output stream. To capture the response into a PHP variable, you must explicitly disable this default behavior using CURLOPT_RETURNTRANSFER. This option ensures that the result is returned as a string rather than being printed directly to the browser or error log.

$ch = curl_init('http://www.example-webpage.com/file.html');

// Disable output transfer and return the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Optional: Prevent following redirects if you only want the specific URL
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

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

if ($response === false) {
    // Handle errors (e.g., network issues or SSL failures)
    $error = curl_error($ch);
    echo "cURL Error: $error";
} else {
    // $response now contains the HTML source code
    echo $response;
}

// Always clean up resources
curl_close($ch);

One of the most common pitfalls developers encounter when fetching external content is handling SSL certificate verification. By default, cURL verifies the peer's SSL certificate against the system's trusted CA bundle. However, some hosting environments or network configurations might have outdated certificates or misconfigured firewalls that cause valid requests to fail silently or throw errors. Disabling SSL verification (CURLOPT_SSL_VERIFYPEER) is often seen as a quick fix, but it introduces significant security risks. It allows your application to connect to any server presenting a certificate signed by any authority, effectively bypassing one of the primary defenses against man-in-the-middle attacks. You should only disable this if you are certain the network environment is trusted or if you are working with an internal API that uses self-signed certificates.

Another consideration is the user agent string. Many modern websites include logic to detect and block requests originating from curl scripts, often returning a 403 Forbidden status or a custom error page instead of the actual content. To mitigate this, you can set the CURLOPT_USERAGENT option to mimic a standard browser like Chrome or Firefox. This increases the likelihood that the server will treat your request as legitimate traffic.

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');

By combining CURLOPT_RETURNTRANSFER with proper error handling and optional header spoofing, you can reliably fetch HTML source code even when file system access is restricted. This approach provides a direct alternative to URL wrappers without requiring changes to your hosting environment's security settings.

Our network