M
M
Mitya ToDaSyo2018-12-06 01:09:21
PHP
Mitya ToDaSyo, 2018-12-06 01:09:21

How to form a request using the execute method in VK api?

Please explain, I don't understand! I'm trying myself in VK api , my knowledge of php is superficial... I
'm uploading information using curl, example:

$GroupInfo['url'] = 'https://api.vk.com/method/groups.getById';
$GroupInfo['post'] .= 'group_id='.$group_id;
$GroupInfo['post'] .= '&fields=members_count';
$GroupInfo['post'] .= '&access_token='.mc_decrypt($_SESSION['UserInfo']['access_token'], ENCRYPTION_KEY);
$GroupInfo['post'] .= '&v='.$System['api']['version'];
$GroupInfo['ch'] = curl_init($GroupInfo['url']);
curl_setopt ($GroupInfo['ch'], CURLOPT_URL, $GroupInfo['url']);
curl_setopt ($GroupInfo['ch'], CURLOPT_HEADER, 0);
curl_setopt ($GroupInfo['ch'], CURLOPT_NOBODY, 0);
curl_setopt ($GroupInfo['ch'], CURLOPT_RETURNTRANSFER, true);
curl_setopt ($GroupInfo['ch'], CURLOPT_POST, true);
curl_setopt ($GroupInfo['ch'], CURLOPT_POSTFIELDS, $GroupInfo['post']);
curl_setopt ($GroupInfo['ch'], CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($GroupInfo['ch'], CURLOPT_SSL_VERIFYHOST, 0);
$GroupInfo['out']=curl_exec($GroupInfo['ch']);
curl_close($GroupInfo['ch']);
$GroupInfo['get'] = json_decode($GroupInfo['out'], true);

There can be several similar requests to different methods on one page.
As far as I understand, it is better to optimize this business!? My question is how do I use execute
with my queries ? From the description in the documentation, I understood absolutely nothing :( If you can give an example, I would be very grateful. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mitya ToDaSyo, 2018-12-06
@dimastik1986

Found in the internet a clear example, almost figured it out!
the only moment - here I have formed queries:

$code .= 'var out = API.users.get({...});'; 
$code .= 'var out = out + API.groups.getById({...});'; 
$code .= 'var out = out + API.stats.get({...});'; 
$code .= 'return out;';

sent the whole thing
$execute = json_decode(GetVK('https://api.vk.com/method/execute', array(
    'code' => $code,
    'access_token' => mc_decrypt($_SESSION['UserInfo']['access_token'], ENCRYPTION_KEY),
    'v' => $System['api']['version']
)), true);

I got an array at the output $execute['response'], it turns out that at the next level I have 3 arrays that correspond to my 3 requests in turn!? Correctly?
------ I
post it for those who may find it useful
1. a convenient function for requests
function vk_query($url, $params) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURLOPT_IPRESOLVE_V4);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

2. request example
$members_all = array();
for ($i=0; $i<$query_count; $i++){
  $offset_out = $i*25000;
    $query = "
      var members = API.groups.getMembers({\"group_id\": $group_vk_id, \"count\":1000, \"fields\": \"sex, bdate\", \"offset\": $offset_out}).items;
      var offset = 1000;
      while (offset < 25000 && (offset+$offset_out < $group_vk_count)){
      members = members + API.groups.getMembers({\"group_id\": $group_vk_id, \"count\":1000, \"fields\": \"sex, bdate\",\"offset\": ($offset_out + offset)}).items;
      offset = offset + 1000;
      };
      return members;
    ";
  $result = json_decode(vk_query('https://api.vk.com/method/execute', array(
    'access_token' => $_SESSION['access_token'],
    'code' => "$query",
    'v' => '5.37'
  )), true);
  //echo '<pre>';
  //print_r($result);
  $members_all[] = $result;
}

source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question