这是代码:
{ "businessName" : "", "businessWebsite" : "", "city" : "Mountain View", "continent" : "North America", "country" : "United States", "countryCode" : "US", "ipName" : "google-public-dns-a.google.com", "ipType" : "Residential", "isp" : "Google", "lat" : "37.3860", "lon" : "-122.0838", "org" : "Google Inc.", "query" : "8.8.8.8", "region" : "California", "status" : "success" } 1
我有这个输出:
Mountain View
但我需要这个(只有城市或任何孤立的价值)
{{1}}
答案 0 :(得分:1)
您只需要解码JSON然后访问类属性:
$ipInfo = json_decode($info);
$city = $ipInfo->city;
答案 1 :(得分:1)
$json = json_decode($info, true);
echo $json['city'];
或者:
<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = json_decode(file_get_contents("http://extreme-ip-lookup.com/json/$user_ip"));
$country = $geo->country;
$city = $geo->city;
$ipType = $geo->ipType;
$businessName = $geo->businessName;
$businessWebsite = $geo->businessWebsite;
echo "Location $city";
?>
答案 2 :(得分:0)
使用参数true
查看json_decode
以获取数组。
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$url = "http://extreme-ip-lookup.com/json/".$ip;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
$info = curl_exec($curl);
curl_close($curl);
$array = json_decode($info, true);
echo $array['city'];