Jump to content

Strange file_get_contents failure. Need help!


Recommended Posts

Hello all. I am having a little problem and I was wondering if someone could help me?

 

So basically, I have a hosting account with hostgator. On my website, I used the php function file_get_contents() in order to get the ip address from a website. It worked for the past week, but just suddenly I am getting a "failure to connect to socket" error message.

 

Here is all that I am trying to do:

 

$d = file_get_contents("http://ipinfodb.com/ip_query.php?ip=$ip&output=xml");

 

I tried without the "&output=xml" and it still doesn't connect. All I can think of is that something got blocked.. But I didn't do anything so maybe it was my host?

 

If anyone has any ideas, it would be very much appreciated. Thanks!

Thank you but I try this:

 

$ch = curl_init();

 

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.

curl_setopt($ch, CURLOPT_URL, $url);

 

$data = curl_exec($ch);

curl_close($ch);

 

return $data;

}

 

 

 

$d = file_get_contents_curl("http://ipinfodb.com/ip_query.php?ip=$ip&output=xml");

 

 

 

 

 

but I still get same error.

 

edit: Actually, now I don' get any error at all, but it still doesn't work.

Works fine for me. Try this, and see what you get:

 

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (ini_get('allow_url_fopen') != '1') {
echo 'allow_url_fopen is Off<br />';
}
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.1) Gecko/20090624 Firefox/3.5');
$ip = '127.0.0.1';

$data = file_get_contents("http://ipinfodb.com/ip_query.php?ip=$ip&output=xml");
if ($data === false) {
echo 'file_get_contents() failed';
} else {
echo $data;
}
?>

Else try with cURL:

 

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$ip = '127.0.0.1';
$url = "http://ipinfodb.com/ip_query.php?ip=$ip&output=xml";

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; da; rv:1.9.1) Gecko/20090624 Firefox/3.5');
$data = curl_exec($ch);
if ($data === false) {
echo 'cURL failed: ' . curl_error($ch);
} else {
echo $data;
}
curl_close($ch);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.