📜 ⬆️ ⬇️

We find common friends of people using VK API

image
0. Prehistory

Hi, Habrayuzer.
Once I had a need to find a person, knowing his appearance and knowing about his membership in a certain club. I also owned the addresses (VKontakte) of the pages of two other members of the club. Almost certainly the desired person was each of them as friends. This problem could be solved in different ways. In the article I will write about how I implemented the solution using the vk.com API.

1.Task

Make a service that will find all the common friends of two separate users, without requiring verification and an account on the social network for users of the service. Get data about mutual friends:

Implement the task on the basis of API vkontakte. Write a hotel class for this.


2. Decision

2.1. Determine the required API methods

Go to the list of API methods . And searching, we find what we need.
')
For receiving friends of the user there is a friends.get method.
friends.get - returns a list of identifiers of friends of the user or extended information about the friends of the user (using the fields parameter).

And, importantly, as part of the solution of the task, this is an open method that does not require an access_token.

Only one required parameter:
image

Using this method, you can get information about all the friends of each of our two sets, but this approach will not be optimal. User A may have 2000 people as friends; only 3 people will intersect with friends. In this case, the information about 1997 users will be unnecessary to us, and the resources spent on getting it will be wasted.
We will only request user IDs, and having received the numbers we need (which belong to the set A and B), we will select information by them.

For information about the user there is a users.get method.
users.get - Returns extended information about users.

This method also does not need access_token, that is, it is perfect for our task.
image
In user_ids, we pass an array of user IDs that are found in both sets.
We want to get only an avatar size of 100 * 100, for this, we give the value photo_100 in the fields parameter.

2.2. Moving to the practical side

I wrote a simple php class:

class VkFriends { public function clean_var($var) { $var = strip_tags($var); $var = preg_replace('~\D+~', '', $var); $var = trim($var); return $var; } public function get_friends($u_id) { $friends = file_get_contents('https://api.vk.com/method/friends.get?user_id='.$u_id); $friends = json_decode($friends); if(!isset($friends->error)){ return $friends; }else{ return ''; } } public function mutual_friends($friends) { $mutual = array_intersect($friends[0]->response, $friends[1]->response); if(!empty($mutual)){ return $mutual; }else{ return ''; } } public function get_users_info($users) { $u_ids = implode(",",$users); $u_info = file_get_contents('https://api.vk.com/method/users.get?user_ids='.$u_ids.'&fields=photo_100'); $u_info = json_decode($u_info); return $u_info; } public function view_user_info($u_info) { $uid = $u_info->uid; $first_name = $u_info->first_name; $last_name = $u_info->last_name; = $u_info->photo_100; print(" <a href='http://vk.com/id$uid' target='_blank'> <div id='info'> <div id='ava'> <img src=''> </div> <div id='name'> $first_name <br/> $last_name </div> </div> </a> "); } public function view_users_info($users_info) { for($i=0;$i<sizeof($users_info->response);$i++){ $this->view_user_info($users_info->response[$i]); } } } 


Now we look at our class in action:

 $vkf = new VkFriends; $u_id[0] = $vkf->clean_var($_POST["u1"]);//clean variables from POST $u_id[1] = $vkf->clean_var($_POST["u2"]); if(($u_id[0]!='')&&($u_id[1]!='')){ echo '<div id="block">'; $friends[0] = $vkf->get_friends($u_id[0]);//getting friends list from user with u_id $friends[1] = $vkf->get_friends($u_id[1]); if(($friends[0]!='')&&($friends[1]!='')){ $mutual = $vkf->mutual_friends($friends);//create new array from intersect arrays if($mutual!=''){ $users_info = $vkf->get_users_info($mutual);//getting info about users that are mutual $vkf->view_users_info($users_info);//view information about selected users }else{ print("<center><h2 class='error'>  </h2></center>"); } }else{ print("<center><h2 class='error'>    </h2></center>"); } echo '</div>'; } 


3. Summary

Link to the GitHub repository - vkfriends .

Thanks for reading,
I sincerely hope that you were interested.

Source: https://habr.com/ru/post/212405/


All Articles