Current Location: Home> Latest Articles> How to Use PHP file_get_contents to Fetch Webpage Content from a Remote URL Explained

How to Use PHP file_get_contents to Fetch Webpage Content from a Remote URL Explained

gitbox 2025-06-09

1. What is file_get_contents?

file_get_contents is a function that reads an entire file into a string. It is very convenient for handling local files but also works with remote HTTP/HTTPS resources.

The syntax is as follows:

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )

Here, $filename can be either a file path or a URL.


2. Basic Usage: Fetching the Content of a Webpage

Let's start with the simplest example, directly fetching the HTML content of a webpage:

<?php
$url = "https://gitbox.net/sample-page.html";
$content = file_get_contents($url);
echo $content;
?>

In this example, file_get_contents sends an HTTP GET request to retrieve the content of https://gitbox.net/sample-page.html and assigns it to $content. Then, echo outputs the webpage's HTML.


3. Adding Custom Headers: Using stream_context_create

Some websites require specific request headers such as User-Agent or Referer. We can create a context using stream_context_create:

<?php
$url = "https://gitbox.net/api/data.json";
$options = [
    "http" => [
        "header" => "User-Agent: PHP\r\n"
    ]
];
$context = stream_context_create($options);
$content = file_get_contents($url, false, $context);
echo $content;
?>

In this code, we simulate a browser request by setting the User-Agent header to avoid having our request rejected by the target server.


4. Handling HTTPS Certificate Verification Issues

When requesting HTTPS resources with file_get_contents, SSL certificate verification may fail. You can disable verification by configuring the context (not recommended for production):

<?php
$url = "https://gitbox.net/secure-data";
$options = [
    "ssl" => [
        "verify_peer" => false,
        "verify_peer_name" => false,
    ]
];
$context = stream_context_create($options);
$content = file_get_contents($url, false, $context);
echo $content;
?>

This method is suitable for debugging or testing environments. In production, use valid certificates and enable verification.


5. Handling Read Failures

If the URL is invalid or the request fails, file_get_contents returns false. You can use the @ error suppression operator along with isset or empty for basic checks:

<?php
$url = "https://gitbox.net/invalid-page";
$content = @file_get_contents($url);
if ($content === false) {
    echo "Request failed, unable to retrieve content.";
} else {
    echo $content;
}
?>

Additionally, you can use error_get_last() to get details about the failure for debugging purposes.


6. Comparison with cURL

While file_get_contents is easy to use, cURL offers more powerful features for complex HTTP requests such as POST, cookies, timeout control, etc. If you need more control over the request behavior, cURL is recommended. But for simple GET requests, file_get_contents is sufficient.


7. Common Issues Summary

  1. allow_url_fopen is false and cannot access URLs?
    Solution: Modify the php.ini file to enable this option:

    allow_url_fopen = On
    
  2. Content appears garbled?
    Check the encoding of the target page and convert if necessary:

    $content = mb_convert_encoding($content, 'UTF-8', 'GBK');
    
  3. Cannot access HTTPS?
    Make sure the OpenSSL extension is enabled:

    extension=openssl