📜 ⬆️ ⬇️

Receiving and counting the number of VK.COM reposts

The other day there was a task to count the number of users of the site vk.com, which was shared by a certain post (i.e. the number of reposts). To do this, there is the application " Virus Monitor ", but in the process of using it turned out that it does not consider a large number of reposts (from 100 or more). The slider reaches almost to the end and stops there. And since If a contest was announced for the largest number of reposts for a certain record, then an alternative was needed. Which was not ...

Therefore, I had to contact the Vkontakte API and look for how to implement this task. I’ll say right away that I didn’t have to create standalone applications for this purpose. Everything turned out to be implemented using methods that do not require an access_token . Below is a list of API methods used in this task:
From the advantages of this calculation, you can only select what you do not need to create an application for this purpose.
From the minuses (if you study these API methods): inconvenient search for our repost from the user. Vkontakte allows you to receive a maximum of 100 news using the wall.get method. Of course, this method supports the offset parameter (news offset), but I still limited myself to a certain amount of news to process (500 pieces).

Some problems encountered:

So, we needed data about those users and their posts that shared our repost. To get this list, use the likes.getList API method , which has a filter parameter that takes two possible values:
But when something was ready, it turned out that the data obtained using the API do not converge with the real ones.
')
For example, a user who “shared” did not get the likes.getList? Filter = copies method, but was on the list who simply “liked” the post. Besides, another user came across with the situation exactly the opposite.

Therefore, for more accurate results, it came to process the entire list (users who click "Like" and "Share").

Below is a class written in PHP for finding the number of reposts:

Show code
class VKLIKES { private $url = 'https://api.vk.com/method/'; // URL   API private $owner_id; //ID   private $post_id; //ID  private $count = 1000; //  ""  ""  private $users = array(); //   private $countReposts; //     private $findPost; //ID      private $find; // /     public function __construct($owner_id = '', $post_id = '') { $this->owner_id = $owner_id; $this->post_id = $post_id; } //   private function printProgress($text, $start = true, $error = false) { if ($error) $color = 'red'; else if ($start) $color = '#444'; else $color = 'green'; echo '<li style="color: '.$color.';">'.$text.'</li>'; ob_flush(); flush(); } /*  ,     * $onwer_id - ID   * $post_id - ID- * $fitler - "likes"  "copies" * $offset -   .    1000  * $onlyCount -     * $start -    */ private function getUsers($owner_id, $post_id, $filter, $offset = 0, $onlyCount = false, $start = true) { // URL    $url = $this->url.'likes.getList?type=post&friends_only=0&offset='.$offset.'&count='.$this->count.'&owner_id='.$owner_id.'&item_id='.$post_id.'&filter='.$filter; //    JSON- $json = file_get_contents($url); // JSON    $data = json_decode($json, true); // ,     if (!isset($data['response'])) return false; $response = $data['response']; $count = $response['count']; //   //,          if ($onlyCount) { $this->countReposts = $count; return true; } //       ,    .    offset. $users = $response['users']; if (count($users) == 0) return false; if ($start) { $this->users = $users; } else { $this->users = array_merge($this->users, $users); } $offset += $this->count; $this->getUsers($owner_id, $post_id, $filter, $offset, $onlyCount, false); } //      .   - ID   vk.com private function remakeUsersArray($usersWithInfo) { $new = array(); foreach ($usersWithInfo as $value) { $new[$value['uid']] = $value; } return $new; } /*     * $vkIDs -   ID  */ function getUsersInfo($vkIDs) { //$count = 1000; //    ,   ID (ID     , ) foreach ($vkIDs as $key => $val) { if ((int)$val < 0) unset($vkIDs[$key]); } $uids = implode(',', $vkIDs); $fields = 'uid,first_name,last_name,nickname,screen_name,sex,city,country,timezone,photo,photo_medium,photo_big,has_mobile,rate,online,counters'; $url = $this->url.'users.get?&uids='.$uids.'&fields='.$fields.'&name_case=nom'; $json = file_get_contents($url); $data = json_decode($json, true); if (isset($data['response'])) { $response = $data['response']; return $response; } return 0; } //   private function getUsersPosts($owner_id, $offset = 0) { $maxNews = 600; //     $count = 100; //100 -    ,       //  $maxNews     if ($offset > $maxNews - $count) { $this->printProgress('<b>     '.$maxNews.' ...</b>', false, true); $this->find = false; return false; } // URL $url = $this->url.'wall.get?'; $url .= 'owner_id='.$owner_id.'&'; $url .= 'offset='.$offset.'&'; $url .= 'count='.$count.'&'; $url .= 'filter=owner'; $json = file_get_contents($url); // JSON- $data = json_decode($json, true); //    ""   if (!isset($data['response'])) { $this->printProgress('<b>  </b>', false, true); $this->find = false; return false; } $response = $data['response']; $this->printProgress('    '.($count + $offset).' ..'); // $count  foreach ($response as $news) { if (!is_array($news)) continue; /* copy_owner_id - ID     * copy_owner_id - ID   */ if (isset($news['copy_owner_id'], $news['copy_post_id']) and $news['copy_owner_id'] == $this->owner_id and $news['copy_post_id'] == $this->post_id) { $this->users[$news['from_id']]['repost_id'] = $news['id']; $this->printProgress('<b>    #'.$news['id'].'</b>', false); $this->findPost = $news['id']; $this->find = true; return true; } } $offset += $count; //  $this->getUsersPosts($owner_id, $offset); // } //  function findReposts() { echo '<ul>'; $this->printProgress('  ID ,  ...'); $this->getUsers($this->owner_id, $this->post_id, 'copies'); $this->printProgress(' ID  ', false); $copies = $this->users; $this->printProgress('  ID ,  ...'); $this->getUsers($this->owner_id, $this->post_id, 'likes'); $this->printProgress(' ID  ', false); foreach ($this->users as $id) { if (in_array($id, $copies)) continue; $copies[] = $id; } $this->users = $copies; $this->printProgress('<b> ID     : '.count($this->users).'</b>'); $this->printProgress('      ID...'); $usersWithInfo = $this->getUsersInfo($this->users); $this->printProgress('   ', false); $this->printProgress('<b> ID   : '.count($usersWithInfo).'</b>'); $this->printProgress('     ...'); $this->users = $this->remakeUsersArray($usersWithInfo); $this->printProgress('  ', false); $this->printProgress('    ...'); $k = 1; foreach ($this->users as $id => $data) { $this->printProgress('<i>'.$k.')  : <a href="http://vk.com/id'.$id.'">id'.$id.'</a> - '.$data['last_name'].' '.$data['first_name'].'</i>'); $this->getUsersPosts($id); if ($this->find) { $this->printProgress('   #'.$this->findPost.'  ...'); $this->getUsers($id, $this->findPost, 'copies', 0, true); $status = '<span'; if ($this->countReposts > 0) $status .= ' style="font-size: 20px;"'; $status .= '>  #'.$this->findPost.': <b>'.$this->countReposts.'</b></span>'; $this->printProgress($status, false); $this->users[$id]['count_reposts'] = $this->countReposts; //   $this->user[$id]   } $k++; } $this->printProgress('  ', false); } } 



The above class allows you to get the number of reposts with user data. Similarly, you can count and likes (you just need to install? Filter = likes)
This solution is a very convenient way to display data when embedded on your website, when conducting various quizzes and contests. For example, we implemented it this way.

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


All Articles