2012-06-03 29 views
0

这是一个有用的代码片段,用于检测特定IP地址的位置。以下功能将一个IP地址作为参数,并返回IP地址的位置。如果找不到位置,则返回UNKNOWN。通过IP地址检测位置(为什么?)

但我得到一个空白页。为什么?

function detect_city($ip) { 
    $default = 'UNKNOWN'; 

    if (!is_string($ip) || 
     strlen($ip) < 1 || 
     $ip == '127.0.0.1' || 
     $ip == 'localhost') 

     $ip = '8.8.8.8'; 

    $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2)  Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'; 

    $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); 
    $ch = curl_init(); 

    $curl_opt = array(
     CURLOPT_FOLLOWLOCATION => 1, 
     CURLOPT_HEADER  => 0, 
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_USERAGENT => $curlopt_useragent, 
     CURLOPT_URL  => $url, 
     CURLOPT_TIMEOUT   => 1, 
     CURLOPT_REFERER   => 'http://' . $_SERVER['HTTP_HOST'], 
    ); 

    curl_setopt_array($ch, $curl_opt); 

    $content = curl_exec($ch); 

    if (!is_null($curl_info)) { 
     $curl_info = curl_getinfo($ch); 
    } 

    curl_close($ch); 

    if (preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs)) { 
     $city = $regs[1]; 
    } 
    if (preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs)) { 
     $state = $regs[1]; 
    } 

    if ($city!='' && $state!='') { 
     $location = $city . ', ' . $state; 
     return $location; 
    } 
    else { 
     return $default; 
    } 
} 
+1

你怎么称呼这种方法吗?请显示完整的代码源。 – Ruben

+1

而不是搞乱页面抓取,你应该考虑使用他们的API http://ipinfodb.com/ip_location_api.php。 PHP代码提供,用户密钥是免费的。 – fvu

+0

谢谢@ fvu我使用这个来源,工作正常,但我只需要显示我的唯一countryCode什么可以做到这一点?! – user1433591

回答

0

这条线:通过看上面的参数,其余代码

if (!is_null($curl_info)) { 
    $curl_info = curl_getinfo($ch); 
} 

$ curl_info不存在,cannot be。取而代之的是,发表意见if出来,你应该罚款(我的IP地址进行测试):

// if (!is_null($curl_info)) { 
    $curl_info = curl_getinfo($ch); 
// } 
+0

我再次获得空白页面,您可以发布您在ip上测试的完整鳕鱼吗? – user1433591

+1

非常相同的代码,只是评论这两行......但你可以启用错误报告,看看你是否有问题:error_reporting(E_ALL);的ini_set( “display_errors设置”,1);在页面的顶部...你也可以在preg_match之前检查$ content是否包含任何东西(echo $ content).. – 2012-06-03 14:34:27

3

你可能会得到一个空白页,因为在你的脚本错误,PHP未配置为显示错误,大多数在线网站都是这种情况。尝试通过在同一目录下添加这一个.htaccess文件,以使错误显示:

php_flag display_errors on 
php_value error_reporting 7 
0

希望这有助于:)

function detect_city($ip) { 

    $default = 'UNKNOWN'; 

    if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost') 
     $ip = '8.8.8.8'; 

    $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)'; 

    $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip); 
    $ch = curl_init(); 

    $curl_opt = array(
     CURLOPT_FOLLOWLOCATION => 1, 
     CURLOPT_HEADER  => 0, 
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_USERAGENT => $curlopt_useragent, 
     CURLOPT_URL  => $url, 
     CURLOPT_TIMEOUT   => 1, 
     CURLOPT_REFERER   => 'http://' . $_SERVER['HTTP_HOST'], 
    ); 

    curl_setopt_array($ch, $curl_opt); 

    $content = curl_exec($ch); 

    if (!is_null($curl_info)) { 
     $curl_info = curl_getinfo($ch); 
    } 

    curl_close($ch); 

    if (preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs)) { 
     $city = $regs[1]; 
    } 
    if (preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs)) { 
     $state = $regs[1]; 
    } 

    if($city!='' && $state!=''){ 
     $location = $city . ', ' . $state; 
     return $location; 
    }else{ 
     return $default; 
    } 

}