PHP- file_get_contents failed to open stream: Connection refused
Asked Answered
G

5

14

I am using the following API for getting the country code using IP

http://api.hostip.info/country.php?ip=' . $IP

Example: on Localhost

$IP = '202.71.158.30';

//pass the ip as a parameter for follow URL it will return the country

$country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);

and its working fine here and showing the country code.

But it showing error on Server

Example:

$IP=$_SERVER['REMOTE_ADDR'];

$country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $IP);

Showing following error:

Warning: file_get_contents(http://api.hostip.info/country.php?ip=101.63.xx.xxx) [function.file-get-contents]: failed to open stream: Connection refused in /srv/disk4/1322145/www/servername.in/app/header.php on line 12

Whats wrong with this?

Glycoside answered 5/4, 2013 at 13:30 Comment(11)
Probably this has little to do with PHP and more with network restrictions. Try dumping $http_response_header after file_get_contents to get more info about why it failed.Qp
Make sure that you can open a url with fopen. See: php.net/…Longawa
@MarcellFülöp : But its working on localhost only the error showing on server. So is it the issue of $_SERVER['REMOTE_ADDR']; ?Glycoside
@MarcellFülöp : var_dump($country_code); showing bool(false)Glycoside
I meant $http_response_header. This variable gets populated by PHP when file_get_contents is used with the HTTP wrapper and might give useful information about the HTTP request.Qp
@MarcellFülöp: you mean var_dump($http_response_header); right?Glycoside
Yes. That should be an array, each element is a line of the response headers of the resulting HTTP response.Qp
@MarcellFülöp: Its showing NULLGlycoside
@MarcellFülöp : $country_code = file_get_contents('http://api.hostip.info/country.php?ip=' . $ip); var_dump($http_response_header);Glycoside
I expected that. There's no response, because the request didn't make through to the server. Check if allow URL fopen is on in the given PHP setup, and try wget -d from a terminal. That should give you more info about why it cannot connect to that server.Qp
Try wget -d -O /dev/null "http://api.hostip.info/country.php?ip=[an IP]" and see the output.Qp
C
14

You can use CURL in place of file_get_contents()

<?php
    $IP = '202.71.158.30'; 
    $runfile = 'http://api.hostip.info/country.php?ip=' . $IP;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $runfile);

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

    $content = curl_exec ($ch);

    curl_close ($ch); 

    echo $content;
Cyndi answered 5/4, 2013 at 14:6 Comment(0)
S
4

Use curl method instead of file_get_contents()

It does not work on localhost at google.com. Instead use other engine or use curl below:

Make a function or use inside the same function:

I prefer it to use in a function to avoid repetition in future.

$IP = '202.71.158.30';
$country_code = file_get_contents_fun('http://api.hostip.info/country.php? 
ip='.$IP);
public function file_get_contents_fun($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT,30);
    $content = curl_exec ($ch);
    curl_close ($ch); 
    return $content;
}
Sb answered 14/1, 2023 at 7:13 Comment(0)
A
3

Some servers do not permit accessing through IP address in your request. You can use CURL to prevent this problem.

Angelynanger answered 5/4, 2013 at 14:33 Comment(0)
R
3

In my case the Fail2Ban extension in Plesk suddenly started IP-blocking the server that did the file_get_contents() requests. This is probably not going to be the issue, but I just wanted you to be aware of such a possibility.

Rossierossing answered 15/6, 2020 at 8:40 Comment(2)
Spent the past 5 hours fighting with this thing. This helped me. My server was banning itself on postfix! and it was killing the file_get_contents() response.Rina
OMG!!!! I just spent a whole day trying to fix this. Your comment fixed it! Thank You. Saved me so much work. I thought I would have to re-code using cURL. Was working for 12 months then all of a sudden stopped. Fail2Ban was the culprit. Locked out the host server. Thank You again.Biometry
B
1

add optional parameter in file_get_contents using stream_context_create function to open stream connection valid.

$context = stream_context_create([
    'http' => ['protocol_version' => '1.1'],
]);

$result = file_get_contents($url, false, $context);
var_dump($result);
Bravin answered 26/4, 2023 at 4:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.