HTTP Request Failed Issue with file_get_contents()
Encountering the "failed to open stream: HTTP request failed!" error when using file_get_contents() to call a URL can be frustrating. This issue arises when PHP encounters problems establishing a connection to the target server.
One possible cause, as the user suggests, could be the présence of a second "http://" within the URL. However, another potential issue is the lack of support for HTTPS URLs in file_get_contents().
To resolve this problem, consider employing the cURL library instead of file_get_contents(). cURL offers greater flexibility and control over HTTP requests. Here's a code sample using cURL:
<?php $curl_handle=curl_init(); curl_setopt($curl_handle, CURLOPT_URL, 'http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv'); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name'); $query = curl_exec($curl_handle); curl_close($curl_handle); ?>
By incorporating these modifications, you'll be able to establish a stable connection and successfully retrieve the desired content from the URL.
The above is the detailed content of Why is my `file_get_contents()` Failing and How Can I Fix the 'HTTP request failed!' Error?. For more information, please follow other related articles on the PHP Chinese website!