A
A
Artyom Sergeev2021-02-11 12:45:11
PHP
Artyom Sergeev, 2021-02-11 12:45:11

How to calculate the total number of subscribers from several groups in VK?

Tell me there are groups in VK (4 pcs). How to display the number of subscribers (number) on the site page? And then stack with other groups

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
no_one_safe, 2021-02-11
@no_one_safe

VK api and addition operation.

A
Alex K, 2021-02-11
@alexk111

If you need a ready-made solution, then here is the thread for the Botodrome. Returns a page on url /group-statsthat displays the stats for each group and the sum in all groups. In the "VK API data" node, insert the id of the groups for which to display data, in "My account" - your token for accessing the VK API and you're done.

[
    {
        "id": "1a53fed2.b20021",
        "type": "change",
        "z": "398791b.201f36e",
        "name": "Данные VK API",
        "rules": [
            {
                "t": "set",
                "p": "apiData.group_ids",
                "pt": "msg",
                "to": "business,donut",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "apiData.fields",
                "pt": "msg",
                "to": "members_count",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 600,
        "y": 380,
        "wires": [
            [
                "4baee50a.80781c"
            ]
        ]
    },
    {
        "id": "4baee50a.80781c",
        "type": "drom-vk-api-call",
        "z": "398791b.201f36e",
        "name": "Получ инфо о группах",
        "data": "apiData",
        "dataType": "msg",
        "method": "groups.getById",
        "methodType": "str",
        "property": "payload",
        "client": "f6e8a943.b2d388",
        "apiV": "5.126",
        "x": 630,
        "y": 420,
        "wires": [
            [
                "e8e387ac.a92108"
            ]
        ]
    },
    {
        "id": "bb769f50.7776f",
        "type": "drom-template",
        "z": "398791b.201f36e",
        "name": "HTML",
        "field": "payload",
        "fieldType": "msg",
        "syntax": "handlebars",
        "htmlEscape": true,
        "template": "<html>\n<head></head>\n<body>\n<ul>\n{{#each payload}}\n<li>{{this.name}} - {{this.members_count}}</li>\n{{/each}}\n</ul>\n\n<p>\n  Всего: {{total}}\n</p>\n</body>\n</html>",
        "x": 1090,
        "y": 380,
        "wires": [
            [
                "4ebdbb53.73c5f4"
            ]
        ]
    },
    {
        "id": "e7643afd.a46498",
        "type": "http in",
        "z": "398791b.201f36e",
        "name": "",
        "url": "/group-stats",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 360,
        "y": 400,
        "wires": [
            [
                "1a53fed2.b20021"
            ]
        ]
    },
    {
        "id": "4ebdbb53.73c5f4",
        "type": "http response",
        "z": "398791b.201f36e",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 1090,
        "y": 420,
        "wires": []
    },
    {
        "id": "e8e387ac.a92108",
        "type": "change",
        "z": "398791b.201f36e",
        "name": "Сумма всех",
        "rules": [
            {
                "t": "set",
                "p": "total",
                "pt": "msg",
                "to": "$sum(payload.members_count)\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 870,
        "y": 400,
        "wires": [
            [
                "bb769f50.7776f"
            ]
        ]
    },
    {
        "id": "f6e8a943.b2d388",
        "type": "drom-vk-api-config",
        "apiV": "5.126",
        "name": "Мой акк"
    }
]

S
Sergey Sokolov, 2021-02-12
@sergiks

For educational purposes, you can write a code for a procedure executed on the VC side that will receive the counters of the specified groups and add them. And execute this code with the execute() method
Procedure code:

var groups = "apiclub,55293029"; // сюда подставить ваши группы
var counts = API.groups.getById({
  group_ids: groups,
  fields: "members_count",
})@.members_count;
var total = 0;
while (counts.length > 0) total = total + counts.pop();
return total;

You will need a Community token - any of your groups.
Full working code
$code = <<<EOFVK
var groups = "apiclub,55293029";
var counts = API.groups.getById({
  group_ids: groups,
  fields: "members_count",
})@.members_count;

var total = 0;
while (counts.length > 0) total = total + counts.pop();
return total;
EOFVK;

// токен сообщества получить в Настройках своего сообщества, Работа с API
// нажать кнопку Создать ключ и выбрать любую галочку.
$token = '123123123';

$method = 'execute';
$params = [
    'access_token' => $token,
    'v' => '5.130',
];

$post_data = [
    'code' => $code,
];

$url = sprintf(
    'https://api.vk.com/method/%s?%s',
    $method,
    http_build_query($params)
);

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $post_data,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
]);

$curl_result = curl_exec($ch);
$data = json_decode($curl_result);

$count = $data->response;

printf('<h2>Сейчас в группах %d участников</h2>', $count);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question