Php 确定输入中的城市是否有效

Php 确定输入中的城市是否有效,php,city,Php,City,我想确定在文本框中输入的城市用户是否有效,我有以下代码: <!DOCTYPE html> <html> <head> <title></title> </head> <body> <?php $check = checkCityOrCountry('Sweden'); if($check === false) echo 'Not Valid country'; else {

我想确定在文本框中输入的城市用户是否有效,我有以下代码:

<!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(“瑞典”);我总是得到“无效国家”输出。

如果您检查瑞典不存在


已编辑:正确的URL为

您可以如下方式更新该功能:


…如果您没有使用cURL的特定原因,那么只使用file\u get\u contents()更简单。另外,如果要将数据作为数组处理,使用json_decode(),第二个参数应为“true”。

Hmm。您知道其他检查国家/城市是否存在的方法吗?另外,我尝试输入比利时,但输出是相同的。我从函数vkapi()返回的URL是,瑞典在数组中。问题出在checkCityOfCountry()中,其中$validString=false;设置了,但数组检查与properlyn不匹配无论输入是什么,输出总是相同的-无效@Ardit Meti yeah没有正确地看到您的URL。您是否可以变量转储此行:$countries=$countries['response';var_dump返回NULL@迈克尔:没错,它是空的。你的
$title
s是你希望他们在for循环中出现的吗?
$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;
}