PHP- file_get_contents无法打开流:连接被拒绝

J.K*_*.A. 4 php file-get-contents

我使用以下API来获取使用IP的国家/地区代码

http://api.hostip.info/country.php?ip=' . $IP
Run Code Online (Sandbox Code Playgroud)

示例:在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);
Run Code Online (Sandbox Code Playgroud)

它在这里运作良好,并显示国家代码.

但它在服务器上显示错误

例:

$IP=$_SERVER['REMOTE_ADDR'];

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

显示以下错误:

警告:file_get_contents(http://api.hostip.info/country.php?ip=101.63.xx.xxx)[function.file-get-contents]:无法打开流:/ srv/disk4/1322145中的连接被拒绝第12行/www/servername.in/app/header.php

这有什么不对吗?

小智 11

您可以使用CURL代替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;
Run Code Online (Sandbox Code Playgroud)