get url content PHP

get url content PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Handling HTTP Redirection Limits in PHP

When you attempt to fetch a URL using file_get_contents() and encounter a Warning: ... failed to open stream: Redirection limit reached, the issue isn't necessarily about page protection or complex header manipulation as initially suspected. It is almost always a matter of default configuration constraints within the PHP runtime.

The standard file_get_contents() function relies on the cURL library under the hood, but it adheres to strict defaults regarding HTTP redirects. By default, PHP will follow up to 5 redirects. If the target server (like findchips.com in your specific case) sends a redirect chain that exceeds this threshold—common with complex authentication flows or anti-bot measures—the function simply aborts and throws the error you are seeing.

To override this behavior without switching entirely to cURL immediately, you can adjust the global configuration setting for the maximum number of redirects. You need to set default_socket_timeout and specifically handle the redirection limit via the allow_redirects context or by modifying php.ini. However, simply changing max_execution_time or memory_limit won't solve this; you must explicitly tell PHP how many hops to follow.

The most direct way to increase this limit in a script is by using the stream_context_create() function. You can create a custom stream context that instructs the underlying cURL engine to follow more redirects than the default five. Here is how to implement this:

$context = stream_context_create(
    [
        'http' => [
            'max_redirects' => 10, // Increase from the default of 5
            'ignore_errors' => true, // Optional: handle non-2xx responses gracefully
            'user_agent' => 'Mozilla/5.0 (compatible; PHP Script)' // Helps bypass basic bot detection
        ],
        'ssl' => [
            'verify_peer' => false, // Only disable if you have a specific SSL certificate issue
            'verify_peer_name' => false
        ]
    ]
);

$url = 'http://www.findchips.com/avail?part=74ls244';
$content = file_get_contents($url, false, $context);

if ($content === false) {
    // Handle error, check for specific HTTP status codes if needed
    echo "Failed to fetch content.";
} else {
    // Process your string here
    echo $content;
}

In this example, setting 'max_redirects' => 10 allows the request to traverse up to ten intermediate URLs before failing. This is significantly higher than the default and often resolves redirection limit errors caused by aggressive redirect chains. Additionally, adding a user_agent header is a practical step because many servers (like the one in your error log) block requests that look like they are coming from a standard PHP script rather than a browser.

However, there is a significant trade-off to consider. Increasing the redirection limit can expose you to open redirect vulnerabilities or infinite loops if the target server misbehaves. If you find yourself constantly needing to increase these limits, it might indicate that your application is interacting with endpoints designed for human users, which often employ CAPTCHA challenges or bot-detection scripts.

For more complex scenarios where file_get_contents feels too restrictive regarding headers, timeouts, or redirect logic, migrating the request entirely to cURL is a robust alternative. This gives you granular control over every aspect of the HTTP transaction without relying on stream context limitations. If you are working within a Laravel project, handling these raw HTTP requests directly can sometimes be bypassed by utilizing packages like guzzlehttp/guzzle, which offers a modern, fluent interface for managing redirects and authentication headers effectively.

Ultimately, understanding that the error stems from a hardcoded default rather than an inability to override it empowers you to fix the issue immediately. By adjusting the stream context, you align your PHP script's behavior with the expectations of the target server while maintaining the simplicity of file_get_contents() for basic use cases.

Our network