PHP- file_get_contents failed to open stream: Connection refused

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;

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


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.