Answer the question
In order to leave comments, you need to log in
How to get city name by VK user ID?
Hello!
It is necessary to get the name of the city by the ID of the user who wrote in the chat.
It turns out to get the user's data: id, first name and last name, but the city and date of birth do not work.
Here's what's there:
$vk = new vk_api(VK_KEY, VERSION); // созданем экземпляр класса работы с api, принимаем токен и версию api
$data = json_decode(file_get_contents('php://input')); // Получаем и декодируем JSON пришедший из ВК
$id = $data->object->from_id; // Узнаем ID пользователя, кто написал нам
$userInfo = $vk->request("users.get", ["user_ids" => $id]); // получаем информацию об пользователе
$first_name = $userInfo[0]['first_name']; // Имя
$last_name = $userInfo[0]['last_name']; // Фамилия
$fieldsInfo = $vk->request("users.get", ["fields" => $id]);
$city = $fieldsInfo[0]['city']; // Город
$bdate = $fieldsInfo[0]['bdate']; // Дата рождения
Answer the question
In order to leave comments, you need to log in
If the city and country fields exist for the user, they will be returned. If not, they will be empty or not given at all upon request.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question