I
I
Ilnar Kamaletdinov2018-03-03 07:08:08
PHP
Ilnar Kamaletdinov, 2018-03-03 07:08:08

How to display the number of subscribers of a group?

function vk_fans_count($vkID) {
  $json_string = file_get_contents('https://api.vk.com/method/groups.getMembers?group_id=айдишник&count=0&fields=count&access_token=здесь токен&v=5.73');
  $json = json_decode($json_string, true);
  return $json['response'][0]['count'];
}

I write this code in WordPress functions.php,
it seems that when I click on the api link, I get a response in the form of json, I try to display this number, but unfortunately nothing happens
{"response":{"count":10344,"items":[]}}
<?php echo vk_fans_count('айдишник');?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2018-03-03
@pevasik

Your hands should be torn off for file_get_contents .

function vk_fans_count($group_id) {
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://api.vk.com/method/groups.getMembers',
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POSTFIELDS => [
                'group_id' => $group_id,
                'count' => 0,
                'v' => 5.73
            ]
        ]);
        $response = json_decode(curl_exec($curl))->response;
        curl_close($curl);
        return $response->count;
}

D
Dmitry Bay, 2018-03-03
@kawabanga

json['response']['count'] ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question