Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2.  
  3. function request($url, $postfields = []) {
  4.     $ch = curl_init($url);
  5.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  6.     if(!empty($postfields)) {
  7.         curl_setopt($ch, CURLOPT_POST, true);
  8.         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
  9.     }
  10.     $json = curl_exec($ch);
  11.     curl_close($ch);
  12.     return json_decode($json, true);
  13. }
  14. function vkapi($m, $p = [], $o = false) {
  15.     if(!isset($p['access_token'])) $p['access_token'] = '';
  16.     if(!isset($p['v'])) $p['v'] = '5.95';
  17.  
  18.     return request("https://api.vk.com/method/{$m}", $p);
  19. }
  20.  
  21. $lp = vkapi('groups.getLongPollServer', ['group_id' => 0])['response'];
  22. $server = $lp['server'];
  23. $key = $lp['key'];
  24.  
  25. $baseurl = "{$server}?act=a_check&key={$key}&wait=25&mode=2&ts=%d";
  26. $url = sprintf($baseurl, $lp['ts']);
  27.  
  28. while(true) {
  29.     $result = request($url);
  30.  
  31.     if(!is_null($result)) {
  32.         if(isset($result['ts'])) {
  33.             if(isset($result['updates']) && !empty($result['updates'])) {
  34.                 $url = sprintf($baseurl, $result['ts']);
  35.                 $updates = $result['updates'];
  36.  
  37.                 foreach($updates as $key => $u) {
  38.                     if($u['type'] != 'message_new') continue;
  39.                     echo "Получено сообщение\n";
  40.                 }
  41.             } else {
  42.                 $url = sprintf($baseurl, $result['ts']);
  43.             }
  44.         } elseif(isset($result['failed'])) {
  45.             echo "failed ({$result['failed']})\n";
  46.  
  47.             switch($result['failed']) {
  48.                 case 1:
  49.                     $url = sprintf($baseurl, $result['ts']);
  50.                     break;
  51.  
  52.                 case 2: case 3:
  53.                     $lp = vkapi('groups.getLongPollServer', ['group_id' => 0])['response'];
  54.                     $server = $lp['server'];
  55.                     $key = $lp['key'];
  56.  
  57.                     $baseurl = "{$server}?act=a_check&key={$key}&wait=25&mode=2&ts=%d";
  58.                     $url = sprintf($baseurl, $lp['ts']);
  59.                     break;
  60.  
  61.                 default: break;
  62.             }
  63.         } else {
  64.             echo json_encode($result, JSON_UNESCAPED_UNICODE)."\n";
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement