Advertisement
Guest User

vk_api

a guest
Dec 15th, 2018
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.45 KB | None | 0 0
  1. <?php
  2. class vk_api{
  3.     /**
  4.      * Токен
  5.      * @var string
  6.      */
  7.     private $token = '';
  8.     private $v = '';
  9.     /**
  10.      * @param string $token Токен
  11.      */
  12.     public function __construct($token, $v){
  13.         $this->token = $token;
  14.         $this->v = $v;
  15.     }
  16.     /**
  17.      * Отправить сообщение пользователю
  18.      * @param int $sendID Идентификатор получателя
  19.      * @param string $message Сообщение
  20.      * @return mixed|null
  21.      */
  22.     public function sendDocMessage($sendID, $id_owner, $id_doc){
  23.         if ($sendID != 0 and $sendID != '0') {
  24.             return $this->request('messages.send',array('attachment'=>"doc". $id_owner . "_" . $id_doc,'user_id'=>$sendID));
  25.         } else {
  26.             return true;
  27.         }
  28.     }
  29.  
  30.     public function sendMessage($sendID,$message){
  31.         if ($sendID != 0 and $sendID != '0') {
  32.             return $this->request('messages.send',array('message'=>$message, 'peer_id'=>$sendID));
  33.         } else {
  34.             return true;
  35.         }
  36.     }
  37.  
  38.     public function sendOK(){
  39.         ini_set('display_errors','Off');
  40.         echo 'ok';
  41.         $response_length = ob_get_length();
  42.         // check if fastcgi_finish_request is callable
  43.         if (is_callable('fastcgi_finish_request')) {
  44.             /*
  45.              * This works in Nginx but the next approach not
  46.              */
  47.             session_write_close();
  48.             fastcgi_finish_request();
  49.  
  50.             return;
  51.         }
  52.  
  53.         ignore_user_abort(true);
  54.  
  55.         ob_start();
  56.         $serverProtocole = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING);
  57.         header($serverProtocole.' 200 OK');
  58.         header('Content-Encoding: none');
  59.         header('Content-Length: '. $response_length);
  60.         header('Connection: close');
  61.  
  62.         ob_end_flush();
  63.         ob_flush();
  64.         flush();
  65.     }
  66.  
  67.     public function sendButton($sendID, $message, $gl_massiv = [], $one_time = False) {
  68.         $buttons = [];
  69.         $i = 0;
  70.         foreach ($gl_massiv as $button_str) {
  71.             $j = 0;
  72.             foreach ($button_str as $button) {
  73.                 $color = $this->replaceColor($button[2]);
  74.                 $buttons[$i][$j]["action"]["type"] = "text";
  75.                 if ($button[0] != null)
  76.                     $buttons[$i][$j]["action"]["payload"] = json_encode($button[0], JSON_UNESCAPED_UNICODE);
  77.                 $buttons[$i][$j]["action"]["label"] = $button[1];
  78.                 $buttons[$i][$j]["color"] = $color;
  79.                 $j++;
  80.             }
  81.             $i++;
  82.         }
  83.         $buttons = array(
  84.             "one_time" => $one_time,
  85.             "buttons" => $buttons);
  86.         $buttons = json_encode($buttons, JSON_UNESCAPED_UNICODE);
  87.         //echo $buttons;
  88.         return $this->request('messages.send',array('message'=>$message, 'peer_id'=>$sendID, 'keyboard'=>$buttons));
  89.     }
  90.  
  91.     public function sendDocuments($sendID, $selector = 'doc'){
  92.         if ($selector == 'doc')
  93.             return $this->request('docs.getMessagesUploadServer',array('type'=>'doc','peer_id'=>$sendID));
  94.         else
  95.             return $this->request('photos.getMessagesUploadServer',array('peer_id'=>$sendID));
  96.     }
  97.  
  98.     public function saveDocuments($file, $titile){
  99.         return $this->request('docs.save',array('file'=>$file, 'title'=>$titile));
  100.     }
  101.  
  102.     public function savePhoto($photo, $server, $hash){
  103.         return $this->request('photos.saveMessagesPhoto',array('photo'=>$photo, 'server'=>$server, 'hash' => $hash));
  104.     }
  105.  
  106.     /**
  107.      * Запрос к VK
  108.      * @param string $method Метод
  109.      * @param array $params Параметры
  110.      * @return mixed|null
  111.      */
  112.     public function request($method,$params=array()){
  113.         $url = 'https://api.vk.com/method/'.$method;
  114.         $params['access_token']=$this->token;
  115.         $params['v']=$this->v;
  116.         if (function_exists('curl_init')) {
  117.             $ch = curl_init();
  118.             curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  119.                 "Content-Type:multipart/form-data"
  120.             ));
  121.             curl_setopt($ch, CURLOPT_URL, $url);
  122.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  123.             curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  124.             $result = json_decode(curl_exec($ch), True);
  125.             curl_close($ch);
  126.         } else {
  127.             $result = json_decode(file_get_contents($url, true, stream_context_create(array(
  128.                 'http' => array(
  129.                     'method'  => 'POST',
  130.                     'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
  131.                     'content' => http_build_query($params)
  132.                 )
  133.             ))), true);
  134.         }
  135.         if (isset($result['response']))
  136.             return $result['response'];
  137.         else
  138.             return $result;
  139.     }
  140.  
  141.     private function replaceColor($color) {
  142.         switch ($color) {
  143.             case 'red':
  144.                 $color = 'negative';
  145.                 break;
  146.             case 'green':
  147.                 $color = 'positive';
  148.                 break;
  149.             case 'white':
  150.                 $color = 'default';
  151.                 break;
  152.             case 'blue':
  153.                 $color = 'primary';
  154.                 break;
  155.  
  156.             default:
  157.                 # code...
  158.                break;
  159.         }
  160.         return $color;
  161.     }
  162.  
  163.     private function sendFiles($url, $local_file_path, $type = 'file') {
  164.         $post_fields = array(
  165.             $type => new CURLFile(realpath($local_file_path))
  166.         );
  167.  
  168.         $ch = curl_init();
  169.         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  170.             "Content-Type:multipart/form-data"
  171.         ));
  172.         curl_setopt($ch, CURLOPT_URL, $url);
  173.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  174.         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  175.         $output = curl_exec($ch);
  176.         return $output;
  177.     }
  178.  
  179.     public function sendImage($id, $local_file_path)
  180.     {
  181.         $upload_url = $this->sendDocuments($id, 'photo')['upload_url'];
  182.  
  183.         $answer_vk = json_decode($this->sendFiles($upload_url, $local_file_path, 'photo'), true);
  184.  
  185.         $upload_file = $this->savePhoto($answer_vk['photo'], $answer_vk['server'], $answer_vk['hash']);
  186.  
  187.         $this->request('messages.send', array('attachment' => "photo" . $upload_file[0]['owner_id'] . "_" . $upload_file[0]['id'], 'peer_id' => $id));
  188.  
  189.         return 1;
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement