fusilli_jerry89 Posted July 7, 2009 Share Posted July 7, 2009 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 https://forums.phpfreaks.com/topic/165037-strange-file_get_contents-failure-need-help/ Share on other sites More sharing options...
beyzad Posted July 7, 2009 Share Posted July 7, 2009 Hi. You better use CURL cuz is as powerful as file_get_contents(); Link to comment https://forums.phpfreaks.com/topic/165037-strange-file_get_contents-failure-need-help/#findComment-870278 Share on other sites More sharing options...
fusilli_jerry89 Posted July 7, 2009 Author Share Posted July 7, 2009 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 https://forums.phpfreaks.com/topic/165037-strange-file_get_contents-failure-need-help/#findComment-870281 Share on other sites More sharing options...
thebadbad Posted July 7, 2009 Share Posted July 7, 2009 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 https://forums.phpfreaks.com/topic/165037-strange-file_get_contents-failure-need-help/#findComment-870300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.