F
F
fomenko_alexandr2017-04-17 17:13:36
PHP
fomenko_alexandr, 2017-04-17 17:13:36

How to execute execute VK API PHP?

Hello masters. Can you please tell me how to use the execute request in php? I searched but didn't find an example of how it should be sent correctly.
I would be very grateful for the hint)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
webtask-pro, 2018-08-01
@webtask-pro

For example like this:

class DevClass {
  public $group_id = 30666517;
  public $membersGroups = array();
  public $access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  public function devMethod(){
    $opts = ["group_id"=>$this->group_id, "access_token"=>$this->access_token, "fields"=>"photo_50,members_count", "v"=>"5.80"];
    $answer = $this->vk->getMethod("groups.getById", $opts, true);
    //$members_count = $answer->response[0]->members_count;
    $members_count = 45000;
    $members_groups = 0;	//изначально в массиве 0 объектов
    //выполняем цикл пока полученное кол-во участников меньше общего кол-ва участников в группе
    while($members_count > $members_groups){
      usleep(300000);	//задержка на 0.3 сек.
      $answer = $this->getMembers25k($this->group_id, $members_count);
      if($answer->response){
        $new = explode(",",$answer->response);
        $this->membersGroups = array_merge($this->membersGroups, $new);
        //второй раз определяем переменную, уже с новым массивом данных
        $members_groups = count($this->membersGroups);
      }
      else{
        echo "NO RESPONSE";
                print_r($answer);
        die();
      }
    }
    print_r($this->membersGroups);
    die();
  }

  public function getMembers25k($group_id, $members_count) {
    $members_groups = count($this->membersGroups);
    $offset = 1000;
    $code =  'var members = API.groups.getMembers({"group_id": '.$this->group_id.', "v": "5.80", "sort": "id_asc", "count": '.$offset.', "offset": '.$members_groups.'}).items;'
      .'var offset = '.$offset.';'
      .'while (offset < 25000 && (offset + '.$members_groups.') < '.$members_count.')'
      .'{members = members + "," + API.groups.getMembers({"group_id": '.$this->group_id.', "v": "5.80", "sort": "id_asc", "count": '.$offset.', "offset": ('.$members_groups.' + offset)}).items;offset = offset + '.$offset.';};'
      .'return members;';

    $answer = $this->vk->getMethod("execute", ["code" => urlencode($code), "v"=>"5.80", "access_token" => $this->access_token], true);
        return $answer;
  }
}

$dev = new DevClass();
$dev->devMethod();

but you need to understand that here I have already generated an access_token by another class, as well as requests to methods ($this->vk->getMethod), I just wanted to show the logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question