Skip to content

Instantly share code, notes, and snippets.

@Mihonarium
Last active June 25, 2018 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mihonarium/0ba242b419ebd2597a401632075bd634 to your computer and use it in GitHub Desktop.
Save Mihonarium/0ba242b419ebd2597a401632075bd634 to your computer and use it in GitHub Desktop.
<?php
function echo_ok() {
ob_start();
echo 'ok';
$length = ob_get_length();
header('Connection: close');
header("Content-Length: " . $length);
header("Content-Encoding: none");
header("Accept-Ranges: bytes");
ob_end_flush();
ob_flush();
flush();
fastcgi_finish_request();
}
function vk_message_send($user_id, $reply, $token, $doc='') {
$parameters = [
'message' => $reply,
'user_id' => $user_id,
'attachment' => $doc,
'access_token' => $token
];
$curl_result = vk_request('messages.send', $parameters, $token);
return $curl_result;
}
function vk_start_typing($user_id, $token) {
$parameters = [
'type' => 'typing',
'user_id' => $user_id,
'access_token' => $token,
'v' => '5.63'
];
$curl_result = vk_request('messages.setActivity', $parameters, $token);
return $curl_result;
}
function message_send($user_id, $reply, $token, $doc='') {
$curl_result = vk_message_send($user_id, $reply, $token, $doc);
return $curl_result;
}
function vk_request($method, $parameters, $token='') {
$parameters['access_token'] = $token;
$parameters['v'] = '5.73';
return post_request('https://api.vk.com/method/'.$method, $parameters);
}
function post_request($url, $parameters=[]) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_result = curl_exec($ch);
curl_close($ch);
return $curl_result;
}
function post_json($url, $parameters=[]) {
$content = json_encode($parameters);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$curl_result = curl_exec($ch);
curl_close($ch);
return $curl_result;
}
function vkApi_upload($url, $file_name) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => new CURLfile($file_name)));
$json = curl_exec($curl);
curl_close($curl);
return $json;
}
function uploadVoiceMessage($filename, $token, $user_id) {
$getUploadServer = vk_request('docs.getMessagesUploadServer', array('peer_id' => $user_id, 'type' => 'audio_message'), $token);
$uploadServerUrl = json_decode($getUploadServer)->response->upload_url;
$upload_result = vkApi_upload($uploadServerUrl, $filename);
unlink($filename);
$doc = json_decode(vk_request('docs.save', array('file' => json_decode($upload_result)->file, 'title' => 'Voice message'), $token))->response[0];
return "doc".$doc->owner_id.'_'.$doc->id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment