5

Can anyone explain why the following code returns a warning:

<?php
  echo file_get_contents("http://google.com");
?>

I get a Warning:

Warning: file_get_contents(http://google.com): 
failed to open stream: No such file or directory on line 2

See codepad

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Kristy Saulsbury
  • 51
  • 1
  • 1
  • 3

6 Answers6

11

As an alternative, you can use cURL, like:

$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

See: cURL

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
5

Try this function in place of file_get_contents():

<?php

function curl_get_contents($url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

It can be used just like file_get_contents(), but uses cURL.

Install cURL on Ubuntu (or other unix-like operating system with aptitude):

sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart

See also cURL

Adelmar
  • 2,073
  • 2
  • 20
  • 20
2

This is almost certainly caused by the config setting that allows PHP to disable the ability to open URLs using the file handling functions.

If you can change you PHP.ini, try switching on the allow_url_fopen setting. See also the man page for fopen for more in (the same restictions affect all file handling functions)

If you can't switch on the flag, you'll need to use a different method, such as Curl, to read your URL.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Ok, so I tried running this same code on http://000webhost.com and I got "301 moved". It has `allow_url_fopen` enabled. Any ideas? – Kristy Saulsbury Sep 06 '12 at 06:37
  • If `allow_url_fopen` was disabled, you'd not get the message "file not found". Apart from that, codepad hat the setting enabled: http://codepad.org/dC19DdmI - the issue is the http stream wrapper not being registered. – Gordon Sep 06 '12 at 07:11
1

If you run this code:

<?php
    print_r(stream_get_wrappers());
?>

in http://codepad.org/NHMjzO5p you see the following array:

Array
(
    [0] => php
    [1] => file
    [2] => data
)

Run the same code in on Codepad.Viper - http://codepad.viper-7.com/lYKihI you will see that the http stream has been enabled thus file_get_contents not working in codepad.org.

Array 
( 
    [0] => https 
    [1] => ftps 
    [2] => compress.zlib 
    [3] => php 
    [4] => file 
    [5] => glob 
    [6] => data 
    [7] => http 
    [8] => ftp 
    [9] => phar 
)

If you run your question code above in Codepad.Viper then it open the google page. The difference thus is the http stream that is disable in your CodePad.org and enabled in CodePad.Viper.

To enable it read the following post How to enable HTTPS stream wrappers. Alternatively use cURL.

Community
  • 1
  • 1
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
-1

Try a trailing slash after the hostname.

<?php
    echo file_get_contents("http://google.com/");
?>
John Carlson
  • 321
  • 1
  • 13
-5

you can try using single quotes like this:

file_get_contents('http://google.com');
Michael Haidl
  • 5,384
  • 25
  • 43
Tony07
  • 1