2013-04-05 82 views
4

我用下面的API用於獲取使用IPPHP的的file_get_contents未能打開流:連接被拒絕

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

實例國家代碼:上本地主機

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

及其工作罰款這裏顯示國家代碼。

但它顯示服務器上的錯誤

例子:

$IP=$_SERVER['REMOTE_ADDR']; 

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

顯示以下錯誤:

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

請告訴我問題呢?

+0

也許這與PHP和更多的網絡限制有關。嘗試在'file_get_contents'之後轉儲'$ http_response_header'以獲取有關失敗原因的更多信息。 – marekful 2013-04-05 13:33:08

+0

確保你可以用fopen打開一個url。請參閱:http://php.net/file_get_contents#refsect1-function.file-get-contents-notes – 2013-04-05 13:33:17

+0

@MarcellFülöp:但它的工作localhost只顯示在服務器上的錯誤。那麼它是$ _SERVER ['REMOTE_ADDR']的問題; ? – Sky 2013-04-05 13:37:11

回答

9

可以替代的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; 
2

有些服務器不允許通過IP地址在您的請求訪問。 您可以使用CURL來防止此問題。

相關問題