Skip to content

Instantly share code, notes, and snippets.

@Nor1m
Last active April 22, 2018 10:55
Show Gist options
  • Save Nor1m/d20ec7a2a85417a7dd406d8aa44a42cd to your computer and use it in GitHub Desktop.
Save Nor1m/d20ec7a2a85417a7dd406d8aa44a42cd to your computer and use it in GitHub Desktop.
<?php
function getDataOnVKAction(){
// копируем массив post
$post_array = $_POST;
// берем настройки
$setting = $post_array['setting'];
// превращаем параметры в обьект
$params_first = json_decode( $post_array['params'] );
// настройки
$link = $setting['link'];
$format = $setting['format'];
$method = $setting['method'];
$only_users = $setting['only_users'];
// вставляем нужные данные
if ($method == "wall.getComments") {
$params_first -> post_id = getIds($link, "post_id");
$first_cell = "items@.from_id";
}
else {
$params_first -> item_id = getIds($link, "item_id");
$first_cell = "items";
}
$params_first -> owner_id = getIds($link, "owner_id");
$limit = $params_first -> count;
// обратно в json
$params_first = json_encode( $params_first );
//debug($params_first, 0);
$api = new VKAPI();
// получаем кол-во элементов
$count_all = $api -> query($method, json_decode( $params_first ), $version = '5.74', $key = 'response', true);
debug("Вк вернул размер массива: " .$count_all, 0);
// кол-во необходимых заходов
$number_of_requests = ceil( $count_all / ( 25 * $limit ) );
// если запросов больше 1, то используем объединение
if ($number_of_requests > 1) $merge = true;
debug("кол-во запросов: " .$number_of_requests, 0);
$array = array();
$array_all = array();
$point_offset = 0;
$params_first = str_replace('"offset":0', '"offset":offset', $params_first);
for ( $i = 0; $i < $number_of_requests; $i++ ) {
$code ='var offset = 0;
var count = '.$count_all.';
var response = [];
var i = 0;
var s = "";
while ( i < 25 && response.length <= count ){
response = response + API.'.$method.'('.$params_first.').'.$first_cell.';
i = i + 1;
offset = '.$limit.' * i;
s = s + "Offset: " + offset + " C: " + i + " Length: " + response.length + "<br>";
}
return s;';
debug($code, 0);
$point_offset += 25000;
$params = array(
'code' => $code,
'version' => "5.74"
);
$array = $api -> query('execute', $params); // отправляем запрос
if ( $merge) $array_all = array_merge($array_all, $array);
else $array_all = $array;
}
debug($array_all , 0);
debug("размер массива до array_unique: ". count($array_all) , 0);
$array_all = array_unique($array_all); // удаляем дубли
debug("размер массива после array_unique: ". count($array_all) );
$api -> showList($array_all, $format, $only_users); // выводим данные
//debug( $array_all );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment