PHP if else语句测试api php字段是否为空
PHP if else statement testing whether api php field is empty
我正在尝试从 ip 地理定位 api 中获取 city
属性。
从 api:
返回的示例
{"as":"AS38484 Virgin Broadband VISP","city":"Adelaide","country":"Australia","countryCode":"AU","isp":"iseek Communications","lat":-27,"lon":133,"org":"iseek Communications","query":"1.178.0.144","region":"","regionName":"","status":"success","timezone":"","zip":""}
我的代码:
$query = '{"as":"AS38484 Virgin Broadband VISP","city":"Adelaide","country":"Australia","countryCode":"AU","isp":"iseek Communications","lat":-27,"lon":133,"org":"iseek Communications","query":"1.178.0.144","region":"","regionName":"","status":"success","timezone":"","zip":""}';
$query = @unserialize($query);
if($query && $query['status'] == 'success') {
if(!empty($query['city'])) {
$city = $query['city'];
// routine that uses $city gets called
} else {
$lat = $query['lat'];
$lon = $query['lon'];
// routine that uses $lat, $lon gets called
}
}
基本上,if(!empty($query['city']))
没有按预期运行(不是我真的知道,上周我一直在使用 PHP)。我还尝试在 if 语句之前设置 $city
,然后测试 if($city != '')
。
问题:没有找到条件组合然后将city属性设置为city
?当没有 city 属性时,它也会跳过 else 部分并且不设置 lat
/lon
.
注意:city
和 lat
/lon
之间的差异的原因是天气 api 我正在查询更喜欢 city
但不是每个ip可以提供一个。
谢谢
$query 不是序列化的 PHP 数组,如果您在 unserialize
调用之前没有使用“@”,您就会看到它。它看起来像 JSON,所以也许 json_decode
就是您要查找的内容?
两个问题:
1)
您需要使用 json_decode
反序列化 json 数据
2)
由于它将反序列化为一个对象,您将使用
访问字段
$query->city;
没有
$query['city'];
正如@kao3991 和@andrew 所说,您的数据是JSON 而不是序列化数组。试试这个:
$query = '{"as":"AS38484 Virgin Broadband VISP","city":"Adelaide","country":"Australia","countryCode":"AU","isp":"iseek Communications","lat":-27,"lon":133,"org":"iseek Communications","query":"1.178.0.144","region":"","regionName":"","status":"success","timezone":"","zip":""}';
$query = json_decode($query, true);
if($query && $query['status'] == 'success') {
if(!empty($query['city'])) {
$city = $query['city'];
// routine that uses $city gets called
} else {
$lat = $query['lat'];
$lon = $query['lon'];
// routine that uses $lat, $lon gets called
}
}
我正在尝试从 ip 地理定位 api 中获取 city
属性。
从 api:
{"as":"AS38484 Virgin Broadband VISP","city":"Adelaide","country":"Australia","countryCode":"AU","isp":"iseek Communications","lat":-27,"lon":133,"org":"iseek Communications","query":"1.178.0.144","region":"","regionName":"","status":"success","timezone":"","zip":""}
我的代码:
$query = '{"as":"AS38484 Virgin Broadband VISP","city":"Adelaide","country":"Australia","countryCode":"AU","isp":"iseek Communications","lat":-27,"lon":133,"org":"iseek Communications","query":"1.178.0.144","region":"","regionName":"","status":"success","timezone":"","zip":""}';
$query = @unserialize($query);
if($query && $query['status'] == 'success') {
if(!empty($query['city'])) {
$city = $query['city'];
// routine that uses $city gets called
} else {
$lat = $query['lat'];
$lon = $query['lon'];
// routine that uses $lat, $lon gets called
}
}
基本上,if(!empty($query['city']))
没有按预期运行(不是我真的知道,上周我一直在使用 PHP)。我还尝试在 if 语句之前设置 $city
,然后测试 if($city != '')
。
问题:没有找到条件组合然后将city属性设置为city
?当没有 city 属性时,它也会跳过 else 部分并且不设置 lat
/lon
.
注意:city
和 lat
/lon
之间的差异的原因是天气 api 我正在查询更喜欢 city
但不是每个ip可以提供一个。
谢谢
$query 不是序列化的 PHP 数组,如果您在 unserialize
调用之前没有使用“@”,您就会看到它。它看起来像 JSON,所以也许 json_decode
就是您要查找的内容?
两个问题:
1)
您需要使用 json_decode
反序列化 json 数据
2) 由于它将反序列化为一个对象,您将使用
访问字段 $query->city;
没有
$query['city'];
正如@kao3991 和@andrew 所说,您的数据是JSON 而不是序列化数组。试试这个:
$query = '{"as":"AS38484 Virgin Broadband VISP","city":"Adelaide","country":"Australia","countryCode":"AU","isp":"iseek Communications","lat":-27,"lon":133,"org":"iseek Communications","query":"1.178.0.144","region":"","regionName":"","status":"success","timezone":"","zip":""}';
$query = json_decode($query, true);
if($query && $query['status'] == 'success') {
if(!empty($query['city'])) {
$city = $query['city'];
// routine that uses $city gets called
} else {
$lat = $query['lat'];
$lon = $query['lon'];
// routine that uses $lat, $lon gets called
}
}