判断输入的城市是否有效
Determine if city in input is valid
我想确定在文本框中输入的城市用户是否有效,我有以下代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$check = checkCityOrCountry('Sweden');
if($check === false)
echo 'Not Valid country';
else
{
echo 'Valid: <pre>';
print_r($check);
echo '</pre>';
}
function checkCityOrCountry($name)
{
$checkCity = $name;
$checkCity = mb_strtolower($checkCity, "UTF-8");
$countries = vkapi('database.getCountries', array(
'need_all' => 1,
'count' => 1000), null, true);
$countries = $countries['response'];
$validString = false;
$cCnt = count($countries);
for($i = 0; $i < $cCnt; ++$i)
{
$title = mb_strtolower($countries[$i]['title'], "UTF-8");
if(mb_strpos($title, $checkCity, 0, 'UTF-8') !== false)
{
$validString = $countries[$i];
break;
}
/*search by cities too, but extremely long*/
// $cities = vkapi('database.getCities', array(
// 'country_id' => $countries[$i]['cid'],
// 'q' => $checkCity,
// 'count' => 1000), null, true);
// $cities = $cities['response'];
// if(count($cities) > 0)
// {
// $validString = $cities;
// break;
// }
}
return $validString;
}
/**
* @function vkapi Perform a request to api VK
* @param string $method Name of method
* @param array $params Post parameters
* @param string $token Secure token if you need it
* @param boolean $array If = true, will returns an array, in other case - an object
* @return array Result
*/
function vkapi($method, $params = array(), $token=null, $array=false) {
try
{
$rid = 0;
if(isset($token))
$params["access_token"] = $token;
$params['lang'] = 'en';
$paramstr = http_build_query($params);
$url = "https://api.vk.com/method/" . $method . "?" . $paramstr;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($ch));
if($array == true)
{
$result = json_decode(json_encode($result), true);
}
return $result;
}
catch(Exception $e)
{
throw new Exception('VK API: '.$e->getMessage());
}
}
?>
</body>
</html>
但是无论我输入什么,这段代码都不起作用
$check = checkCityOrCountry('Sweden');我总是得到 "Not Valid country" 输出。
如果你勾选 https://api.vk.com/method/database.getCountries?need_all=1 那里不存在瑞典..
已编辑:正确的 URL 是 https://api.vk.com/method/database.getCountries?need_all=1&count=1000&lang=en
您可以像这样更新函数:
$check = checkCityOrCountry('Sweden');
echo var_dump($check); // true
$check = checkCityOrCountry('foo');
echo var_dump($check); // false
function checkCityOrCountry($name)
{
$countries = file_get_contents('https://api.vk.com/method/database.getCountries?need_all=1&count=1000&lang=en');
$arr = json_decode($countries, true);
foreach ($arr['response'] as $country) {
if (mb_strtolower($country['title']) === mb_strtolower($name)) {
return true;
}
}
return false;
}
...如果您没有使用 cURL 的特定原因,使用 file_get_contents() 会更简单。此外,使用 json_decode() 如果要将数据作为数组处理,第二个参数应为 'true'.
我想确定在文本框中输入的城市用户是否有效,我有以下代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$check = checkCityOrCountry('Sweden');
if($check === false)
echo 'Not Valid country';
else
{
echo 'Valid: <pre>';
print_r($check);
echo '</pre>';
}
function checkCityOrCountry($name)
{
$checkCity = $name;
$checkCity = mb_strtolower($checkCity, "UTF-8");
$countries = vkapi('database.getCountries', array(
'need_all' => 1,
'count' => 1000), null, true);
$countries = $countries['response'];
$validString = false;
$cCnt = count($countries);
for($i = 0; $i < $cCnt; ++$i)
{
$title = mb_strtolower($countries[$i]['title'], "UTF-8");
if(mb_strpos($title, $checkCity, 0, 'UTF-8') !== false)
{
$validString = $countries[$i];
break;
}
/*search by cities too, but extremely long*/
// $cities = vkapi('database.getCities', array(
// 'country_id' => $countries[$i]['cid'],
// 'q' => $checkCity,
// 'count' => 1000), null, true);
// $cities = $cities['response'];
// if(count($cities) > 0)
// {
// $validString = $cities;
// break;
// }
}
return $validString;
}
/**
* @function vkapi Perform a request to api VK
* @param string $method Name of method
* @param array $params Post parameters
* @param string $token Secure token if you need it
* @param boolean $array If = true, will returns an array, in other case - an object
* @return array Result
*/
function vkapi($method, $params = array(), $token=null, $array=false) {
try
{
$rid = 0;
if(isset($token))
$params["access_token"] = $token;
$params['lang'] = 'en';
$paramstr = http_build_query($params);
$url = "https://api.vk.com/method/" . $method . "?" . $paramstr;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($ch));
if($array == true)
{
$result = json_decode(json_encode($result), true);
}
return $result;
}
catch(Exception $e)
{
throw new Exception('VK API: '.$e->getMessage());
}
}
?>
</body>
</html>
但是无论我输入什么,这段代码都不起作用 $check = checkCityOrCountry('Sweden');我总是得到 "Not Valid country" 输出。
如果你勾选 https://api.vk.com/method/database.getCountries?need_all=1 那里不存在瑞典..
已编辑:正确的 URL 是 https://api.vk.com/method/database.getCountries?need_all=1&count=1000&lang=en
您可以像这样更新函数:
$check = checkCityOrCountry('Sweden');
echo var_dump($check); // true
$check = checkCityOrCountry('foo');
echo var_dump($check); // false
function checkCityOrCountry($name)
{
$countries = file_get_contents('https://api.vk.com/method/database.getCountries?need_all=1&count=1000&lang=en');
$arr = json_decode($countries, true);
foreach ($arr['response'] as $country) {
if (mb_strtolower($country['title']) === mb_strtolower($name)) {
return true;
}
}
return false;
}
...如果您没有使用 cURL 的特定原因,使用 file_get_contents() 会更简单。此外,使用 json_decode() 如果要将数据作为数组处理,第二个参数应为 'true'.