[Solved] PHP file_get_contents() returns “failed to open stream: HTTP request failed!”

I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use file-get-contents() to make the call, I get:

Warning: file-get-contents(http://…. ) failed to open stream: HTTP request failed! HTTP/1.1 202 Accepted in …

The code I am using is:

$query=file_get_contents('http://###.##.##.##/mp/get?mpsrc= format=flv');

Like I said – call from the browser and it works fine. Any suggestions?

I have also tried with another url such as:


This works fine… could it be that the url I need to call has a second http:// in it?

Solution #1:

Try using cURL.


curl_setopt($curl_handle, CURLOPT_URL,'http://###.##.##.##/mp/get?mpsrc= 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);

Respondent: James Hall

Solution #3:



function get_fcontent( $url,  $javascript_loop = 0, $timeout = 5 ) {
    $url = str_replace( "&amp;", "&", urldecode(trim($url)) );

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302) {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

        if ( $headers = get_headers($response['url']) ) {
            foreach( $headers as $value ) {
                if ( substr( strtolower($value), 0, 9 ) == "location:" )
                    return get_url( trim( substr( $value, 9, strlen($value) ) ) );

    if (    ( preg_match("/>[[:space:]]+window.location.replace('(.*)')/i", $content, $value) || preg_match("/>[[:space:]]+window.location="(.*)"/i", $content, $value) ) && $javascript_loop < 5) {
        return get_url( $value[1], $javascript_loop+1 );
    } else {
        return array( $content, $response );

Respondent: pangeli

Solution #4:

file_get_contents() utilizes the fopen() wrappers, therefore it is restricted from accessing URLs through the allow_url_fopen option within php.ini.

You will either need to alter your php.ini to turn this option on or use an alternative method, namely cURL – by far the most popular and, to be honest, standard way to accomplish what you are trying to do.

Respondent: Michael Wales

Solution #5:

You basically are required to send some information with the request.

Try this,

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0rn")); 
//Basically adding headers to the request
$context = stream_context_create($opts);
$html = file_get_contents($url,false,$context);
$html = htmlspecialchars($html);

This worked out for me

Respondent: d_bhatnagar

Solution #6:

I notice that your URL has spaces in it. I think that usually is a bad thing. Try encoding the URL with

$my_url = urlencode("my url");

and then calling


and see if you have better luck.

Respondent: slim

Solution #7:

I got a similar problem , I parsed the youtube url. The code is;

$json_is = "".$this->video_url."&max-results=1&alt=json";
$video_info = json_decode ( file_get_contents ( $json_is ), true );     
$video_title = is_array ( $video_info ) ? $video_info ['feed'] ['entry'] [0] ['title'] ['$t'] : '';

Then I realise that $this->video_url include the whitespace. I solved that using trim($this->video_url).

Maybe it will help you . Good Luck

Respondent: Emre Karata?o?lu

Solution #8:

I’m not sure about the parameters(mpaction, format), if they are specified for the amazonaws page or ##.##.

Try to urlencode() the url.

Respondent: alexn

Solution #9:

$query=file_get_contents('http://###.##.##.##/mp/get?' . http_build_query(array('mpsrc' => ' format=flv')));
Respondent: Sergey

Solution #10:

I got a similar problem.

Due to timeout !

Timeout can be indicated like this :

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencodedrn",
        'method'  => "POST",
        'content' => http_build_query($data2),
        'timeout' => 30,
$context = stream_context_create($options); $retour =
$retour = @file_get_contents("", false, $context);
Respondent: Jerry

Solution #11:

This is what worked for me… I did not use curl.

I was able to access a particular API url via browser, but when used in file_get_contents, it gave the error ” failed to open stream”.

Then, I modified the API URL that I wanted to call by encoding all double quotes with urlencoding and kept everything else untouched.

Sample format is given below:


Then use


Respondent: Soumya Rajiv

Solution #12:

Use this

Respondent: Amit Chawla

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy