A
A
Alex-Broudy2019-08-21 20:21:44
PHP
Alex-Broudy, 2019-08-21 20:21:44

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']; // Фамилия

I'm trying to get values ​​by analogy:
$fieldsInfo = $vk->request("users.get", ["fields" => $id]); 
$city = $fieldsInfo[0]['city']; // Город
$bdate = $fieldsInfo[0]['bdate']; // Дата рождения

But nothing comes out,
tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Derepko, 2019-08-21
@Alex-Broudy


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.

J
jolyguf, 2019-08-25
@jolyguf

$vk->request("users.get", ["id" => $id, "fields" => "city, country, bdate"]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question