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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.