Advertisement
Guest User

Untitled

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