3

I have recently upgraded my website's servers due to high amounts of traffic. On the new servers, some aspects of PHP seem to be broken. I have a very specific code that isn't working. However, due to copyright reasons, I can only show the non-confidential equivalent to you:

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

This code worked absolutely flawlessly before the upgrade, and now some odd setting here or there has prevented this code for working.

To be specific, the file_get_contents function doesn't work at all, regardless of what external URL you put in (file_get_contents('index.php') works fine);

Any help is appreciated!

UPDATE #1
This code does not work either:

<?php
ini_set("allow_url_fopen", "On");
echo file_get_contents('http://www.google.com');
?>

UPDATE #2
This code works...

<?php
    ini_set("allow_url_fopen", "On");
    $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;
?>

... but if I try to do simplexml_load_file($data); bad things happen. Same if I do simplexml_load_file('http://www.google.com')...

VCNinc
  • 747
  • 1
  • 9
  • 25
  • Have you tried using curl? – Andy Gee May 25 '13 at 09:34
  • 2
    [allow_url_fopen](http://ch2.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen) – str May 25 '13 at 09:35
  • Does it work if you use IP address instead of URL? I've had this issue in the past where PHP has been unable to resolve domain names. And do things other than file_get_contents work for external URLs? – Sysyphus May 25 '13 at 09:37
  • @str Update description to respond to your comment – VCNinc May 25 '13 at 09:40
  • @VCNinc `"On"` is not a boolean. What does `ini_set()` return? – str May 25 '13 at 09:45
  • Check that you have the same configuration in php.ini in both machines. Probably some of the permissions are not set correctly in the new configuration as @str said. – Mike May 25 '13 at 10:10
  • Maybe firewall issue. Can initialize an external connection in anyway from your new server ? – GiDo May 25 '13 at 10:19
  • @str I tried `true`, `"true"`, and `1` as well – VCNinc May 25 '13 at 12:26
  • @VCNinc According to the documentation you should use `"1"`. – str May 25 '13 at 14:56
  • @str The one thing I didn't try! `"1"` didn't work either, I still can't get the `simplexml_load_file();` function to work! – VCNinc May 26 '13 at 04:34

4 Answers4

2

First check file_get_contents return value. If the value is FALSE then it could not read it. If the value is NULL then the function itself is disabled.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • hi, if I write `echo file_get_contents("http://www.google.com");`, nothing is echoed. I assume NULL – VCNinc May 25 '13 at 12:17
2

Try CURL instead.

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

The contents will be stored in $data.

Krasi
  • 46
  • 2
1

You could throw in headers

reference file-get-contents

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>
Chris
  • 5,584
  • 9
  • 40
  • 58
JohannesAndersson
  • 4,550
  • 2
  • 17
  • 30
1

I found the answer but the credit goes to Krasi;

I used CURL and then used simplexml_load_string($data);

Thanks for all of your help

VCNinc
  • 747
  • 1
  • 9
  • 25