2017-02-05 5 views
0

나는 freegeoip.net으로 주변을 둘러보고 내 방문자의 IP에 대한 정보를 더 얻으려고합니다. 나는 성공적으로 사용자의 IP에 대한 배열 정보를 검색 할 수 있지만, 따로 배열을 깨고PHP - GeoIP 배열 인쇄

배열 결과 등 IP, 도시, 국가 코드, 개별 변수를 얻을 수가 캔트 :

Array ([ip] => 77.99.179.98 [country_code] => GB [country_name] => United Kingdom [region_code] => ENG [region_name] => England [city] => Gloucester [zip_code] => GL1 [time_zone] => Europe/London [latitude] => 51.8333 [longitude] => -2.25 [metro_code] => 0) 

PHP

<?php 
$ip = "77.99.179.98"; 

$geoip = json_decode(file_get_contents('http://freegeoip.net/json/'.$ip), true); 
print_r($geoip); 

foreach ($geoip as $result) { 
    echo $result['ip']."<br>"; 
} 

?> 

편집 : 이제 Warning: Illegal string offset 'ip' in ... on line 9 오류가 발생하지만 IP 주소의 첫 번째 숫자가 반환됩니다 ... wth?

+0

해당 배열의 결과는 무엇입니까 : 모든 방문자의 정보를 표시 할 경우

<?php $geoip = json_decode(file_get_contents('http://freegeoip.net/json/'.$‌​ip); $ip=$geoip['ip']; $country=$geoip['country_name'];// visitor country echo "<br>".$ip; echo "<br>".$country; ?> 

를? 그것은'$ geoip'의 내용입니까? –

+0

예,'foreach ($ geoip as $ result) {' – Armitage2k

+0

좋아요. 그래서'json_decode ($ geoip, true)'를 사용하여 디코딩해야합니다. 그래서'$ geoip json_decode (file_get_contents ('http : // freegeoip. –

답변

1

마침내!

<?php 
$ip = "77.99.179.98"; 

$ip_data = file_get_contents('http://freegeoip.net/json/'.$ip); 
$data = json_decode($ip_data); 

print_r($data); 

echo "<br><br>"; 

echo htmlspecialchars($data->country_name); 

?> 
0

당신이 easly 개별 변수 얻을 수 있습니다 :

<?php 
$geoip = json_decode(file_get_contents('http://freegeoip.net/json/'.$‌​ip)); 
foreach ($geoip as $result) { 
    echo $result."<br>"; 
} 
?>