Decoding the "failed to open stream: HTTP request failed!" Error in PHP
When you run a script using file_get_contents(), encountering a warning that claims the stream failed to open is often misleading because the underlying issue isn't usually a broken network connection or a missing file. In many cases, the HTTP request actually succeeds, but the server responds with a status code that triggers a failure flag in PHP's default context.
Consider the specific scenario where a browser loads a URL perfectly fine, but the same URL passed to file_get_contents() results in an error message like:
Warning: file_get_contents(...) failed to open stream: HTTP request failed! HTTP/1.1 202 Accepted ...
The key detail here is the status code appended to the warning: HTTP/1.1 202 Accepted. While file_get_contents() returns an empty string (falsely interpreted as a failure) when it encounters certain non-2xx status codes, the error message explicitly states the request was accepted by the server. This usually happens with proxy servers or intermediate gateways that return a 202 status instead of forwarding the response to the client immediately, or in cases where the PHP environment is configured to treat non-successful redirects as failures.
The Hidden Problem: Status Codes and Stream Wrappers
The core issue lies in how the stream wrapper interprets HTTP responses. By default, file_get_contents() attempts to read the response body. If the server responds with a status code outside the standard 2xx range (like 3xx redirects without automatic following, or 4xx/5xx errors), PHP stops reading and throws a "failed to open stream" warning.
However, when you see 202 Accepted, it indicates that the request was processed asynchronously or is being handled by an intermediate proxy. The actual response body might not be available immediately, or the proxy blocks direct access from the script while allowing browser traffic due to different User-Agent headers.
Addressing Double URLs in Query Strings
You mentioned trying a URL with a second http:// inside the query string:
$query = file_get_contents('http://server/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
While nested URLs in query parameters are technically valid, they often cause issues if the target server expects a relative path or if special characters aren't encoded. However, in your specific case, the error message points to the HTTP status code (202 Accepted) rather than a parsing error of the URL itself. The double http:// is unlikely to be the root cause unless the server explicitly rejects it with a 202 or similar proxy response.
To handle this robustly, you should use the stream_context_create() function to customize how PHP handles the request. You can configure the context to automatically follow redirects and ignore certain status codes if your application logic dictates that behavior:
$options = [
'http' => [
'header' => "User-Agent: PHP-Client\r\n",
'ignore_errors' => true, // Treat non-2xx as success for reading body
]
];
$context = stream_context_create($options);
$query = file_get_contents('http://server/mp/get?mpsrc=http%3A%2F%2Fmybucket.s3.amazonaws.com%2F11111.mpg&mpaction=convert&format=flv', false, $context);
Note the URL encoding in the example above. Special characters like & and = must be percent-encoded (%26, %3D) to prevent query string parsing errors.
Why Browsers Work When Scripts Fail
The discrepancy between browser behavior and script behavior often stems from headers. Browsers send a User-Agent header that identifies them as legitimate clients, whereas default PHP scripts might send a generic or missing User-Agent. Some servers (or proxies) configured for services like video conversion check this header. If the header is missing or non-browser-like, the server might return a 202 Accepted with a placeholder response instead of the actual data, triggering the stream error.
If you are working within a Laravel application, consider using the HttpClient facade which provides more granular control over redirects and headers:
use Illuminate\Support\Facades\Http;
$response = Http::get('http://server/mp/get?mpsrc=http%3A%2F%2F...');
$query = $response->body();
This approach abstracts away the stream wrapper limitations and gives you explicit access to the response status code, allowing you to decide whether to treat a 202 as an error or wait for a subsequent request.