Good day, gentlemen. Your help in an easy task is necessary, alas, for me because of a lack of experience it turned out to be difficult.

I work with VK API. I search for groups by name via groups.search (for example, music / games / movies), I get a list of foreach ($data1['response'] as $item) and display the name and picture of the group.

Then I need to get the number of members for each group, this can be done through the groups.getMembers method groups.getMembers typing group_id , but I can get group_id only during foreach ($data1['response'] as $item) . If I do this, then in principle everything works, but the speed drops significantly, and the page starts loading in 3-5 seconds. It even seems to me that the script does not have time to work out to the end, because for some groups I don’t get the number of participants.

Maybe I am doing something wrong, but it seems to me that this is so, and it can be implemented differently, but I don’t know how. I would be grateful for any help or hint in which direction to dig.

Here is my code:

 <?php //Получаю список групп $url1 = file_get_contents("https://api.vk.com/method/groups.search?q=".$_POST['vkgroup']."&access_token=*MYTOKEN*"); //ССылка на получение количество пользователей $url2 = "https://api.vk.com/method/groups.getMembers?group_id="; $data1 = json_decode($url1,true); foreach ($data1['response'] as $item) { //Получаю список участников в группе $url3 = file_get_contents($url2.$item['screen_name']); $data2 = json_decode($url3,true); //Получаю цифрой количество участников $cuser=$data2['response']['count']; //Вывожу количество участников echo $cuser; //Вывожу картинку группы echo "<img src=".$item['photo']."> "; //Вывожу название группы echo $item['name']."<br>"; } ?> 
  • How do you then work with this data? Is it possible to cache them? Is it possible to transfer the receipt of the number of users to the frontend? - br3t
  • @ br3t I don't need to cache it. I have an idea to display the list of groups -> select the appropriate group -> on a new page based on the choice to display the last 20 entries from the community wall via wall.get - Vladimir V.
  • Getting the number of users from the client allows you to parallelize the process, but here you have to look at the limits of VKontakte. - br3t
  • @ br3t please tell us more about it. I want to understand all this. - Vladimir V.
  • With an example for you so far it does not work, so I give a link to the documentation: vk.com/dev/api_requests . You are using PP 4.2, you can try 4.1 or 4.3. - br3t

1 answer 1

Here's a (quite a curve, but working) example I got:

 var f = []; var i = 0; $("li").each(function() { var gid = $(this).attr("data-gid"); var q = i; i++; var groupInfoUrl = "https://api.vk.com/method/groups.getMembers?callback=f[" + q + "]&v=5.60&group_id=" + gid; f[q] = function(result) { $("li").eq(q).find('span').text(result.response.count); } var script = document.createElement('SCRIPT'); script.src = groupInfoUrl; document.getElementsByTagName("head")[0].appendChild(script); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <ul> <li data-gid="20629724">https://vk.com/habr - <span>...</span></li> <li data-gid="71741545">https://vk.com/googlerussia - <span>...</span></li> </ul>