PHP File_Get_Contents() Returns "Failed to Open Stream: Http Request Failed!"

PHP file_get_contents() returns failed to open stream: HTTP request failed!

Try 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);

?>

file_get_contents - failed to open stream: HTTP request failed

The problem is that your url is read literally due to '.

If you use " then the variables in the string will be used as variables but now your string is '...apikey=$api' instead of '...apikey=sdflsdksdksdsdskd'

So change the line

$json_url = file_get_contents('https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey');

To

$json_url = file_get_contents("https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?origin=$origin&destination=$destination&departure_date=$departure_date&return_date=$return_date&number_of_results=$number_of_results&apikey=$apikey");

How to fix php Warning: file_get_contents?failed to open stream: HTTP request failed

If this shows in your page, there's something wrong with your display_errors setting.

Ideally, display_errors should be Off for production machines and you should handle errors by yourself using set_error_handler().

See also: Error logging, in a smooth way

This particular warning can't be stopped unless you make sure the page exists. However, used in isolation, you can use the muffle operator to prevent the warning from appearing:

if (false !== ($contents = @file_get_contents('...'))) {
// all good
} else {
// error happened
}

Note that this will still call your custom error handler

PHP file_get_contents() throws error failed to open stream: HTTP request failed! !doctype html

  1. Never use <? ?>. Use only <?php ?> or <?= ?>
  2. urlencode() use only on values of your parameters, not on the whole parameter's line.
  3. file_get_contents() is not a really good method to receive data from the outer servers. Better use CURL.

    <?php

    // Your URL with encoded parts
    $newURL = 'https://api.qwant.com/api/search/images?count=10&offset=1&q='.urlencode('Spicy Cranberry Cavolo Nero (Kale)');

    // This is related to the specifics of this api server,
    // it requires you to provide useragent header
    $options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT => 'any-user-agent-you-want',
    );

    // Preparing CURL
    $curl_handle = curl_init($newURL);

    // Setting array of options
    curl_setopt_array( $curl_handle, $options );

    // Getting the content
    $content = curl_exec($curl_handle);

    // Closing conenction
    curl_close($curl_handle);

    // From this point you have $content with your JSON data received from API server

    ?>

file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in

file_get_contents (read the tip on blue rectangle) can be easily blocked on server side through php.ini avoid using it. When you want to get data from an other site use curl instead. http://php.net/manual/en/book.curl.php, there are plenty of options that you can use with curl, by playing a bit the following code can work with your url.

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,"https://cgi.ebay.com/ws/eBayISAPI.dll?ViewItemRevisionDetails&item=272908801183");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$data = curl_exec($ch);
curl_close($ch);

and by echoing the $data varible you can see the whole page.

echo $data;

you can try parsing the data from the page by utilizing php DOM Methods and convert them to the data type you want (object class, array etc).

file_get_contents(http://127.0.0.1:8000/storage/config/xxx.ini): failed to open stream: HTTP request failed!

Instead of making an http request to read the file, use the storage_path helper:

'contents' => file_get_contents(storage_path('config/'.$service->config, 'r' )),


Related Topics



Leave a reply



Submit