PHP code example of bazgalev / vk-php-sdk
1. Go to this page and download the library: Download bazgalev/vk-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bazgalev / vk-php-sdk example snippets
$vk = new VK\Client\VKApiClient();
$vk = new VKApiClient('5.101');
$vk = new VKApiClient('5.101', VK\Client\Enums\VKLanguage::ENGLISH);
$oauth = new VK\OAuth\VKOAuth();
$oauth = new VK\OAuth\VKOAuth();
$client_id = 1234567;
$redirect_uri = 'https://example.com/vk';
$display = VK\OAuth\VKOAuthDisplay::PAGE;
$scope = [VK\OAuth\Scopes\VKOAuthUserScope::WALL, VK\OAuth\Scopes\VKOAuthUserScope::GROUPS];
$state = 'secret_state_code';
$browser_url = $oauth->getAuthorizeUrl(VK\OAuth\VKOAuthResponseType::CODE, $client_id, $redirect_uri, $display, $scope, $state);
$oauth = new VK\OAuth\VKOAuth();
$client_id = 1234567;
$redirect_uri = 'https://example.com/vk';
$display = VK\OAuth\VKOAuthDisplay::PAGE;
$scope = [VK\OAuth\Scopes\VKOAuthGroupScope::MESSAGES];
$state = 'secret_state_code';
$groups_ids = [1, 2];
$browser_url = $oauth->getAuthorizeUrl(VK\OAuth\VKOAuthResponseType::CODE, $client_id, $redirect_uri, $display, $scope, $state, $groups_ids);
$oauth = new VK\OAuth\VKOAuth();
$client_id = 1234567;
$client_secret = 'SDAScasd'
$redirect_uri = 'https://example.com/vk';
$code = 'CODE';
$response = $oauth->getAccessToken($client_id, $client_secret, $redirect_uri, $code);
$access_token = $response['access_token'];
$oauth = new VK\OAuth\VKOAuth();
$oauth = new VK\OAuth\VKOAuth();
$client_id = 1234567;
$redirect_uri = 'https://example.com/vk';
$display = VK\OAuth\VKOAuthDisplay::PAGE;
$scope = [VK\OAuth\Scopes\VKOAuthUserScope::WALL, VK\OAuth\Scopes\VKOAuthUserScope::GROUPS];
$state = 'secret_state_code';
$revoke_auth = true;
$browser_url = $oauth->getAuthorizeUrl(VK\OAuth\VKOAuthResponseType::TOKEN, $client_id, $redirect_uri, $display, $scope, $state, null, $revoke_auth);
$oauth = new VK\OAuth\VKOAuth();
$client_id = 1234567;
$redirect_uri = 'https://example.com/vk';
$display = VK\OAuth\VKOAuthDisplay::PAGE;
$scope = [VK\OAuth\Scopes\VKOAuthGroupScope::MESSAGES];
$state = 'secret_state_code';
$groups_ids = [1, 2];
$browser_url = $oauth->getAuthorizeUrl(VK\OAuth\VKOAuthResponseType::TOKEN, $client_id, $redirect_uri, $display, $scope, $state, $groups_ids);
$vk = new VK\Client\VKApiClient();
$response = $vk->users()->get($access_token, [
'user_ids' => [1, 210700286],
'fields' => ['city', 'photo'],
]);
$vk = new VK\Client\VKApiClient();
$address = $vk->photos()->getMessagesUploadServer('{access_token}');
$vk = new VK\Client\VKApiClient();
$photo = $vk->getRequest()->upload($address['upload_url'], 'photo', 'photo.jpg');
$vk = new VK\Client\VKApiClient();
$response_save_photo = $vk->photos()->saveMessagesPhoto($access_token, [
'server' => $photo['server'],
'photo' => $photo['photo'],
'hash' => $photo['hash'],
]);
$vk = new VK\Client\VKApiClient();
$address = $vk->video()->save($access_token, [
'name' => 'My video',
]);
$vk = new VK\Client\VKApiClient();
$video = $vk->getRequest()->upload($address['upload_url'], 'video_file', 'video.mp4');
$vk = new VK\Client\VKApiClient();
$vk->groups()->setLongPollSettings($access_token, [
'group_id' => 159895463,
'enabled' => 1,
'message_new' => 1,
'wall_post_new' => 1,
]);
class CallbackApiMyHandler extends VK\CallbackApi\VKCallbackApiHandler {
public function messageNew($object) {
echo 'New message: ' . $object['body'];
}
public function wallPostNew($object) {
echo 'New wall post: ' . $object['text'];
}
}
$vk = new VK\Client\VKApiClient();
$access_token = 'asdj4iht2i4ntokqngoiqn3ripogqr';
$group_id = 159895463;
$wait = 25;
$handler = new CallbackApiMyHandler();
$executor = new VK\CallbackApi\LongPoll\VKCallbackApiLongPollExecutor($vk, $access_token, $group_id, $handler, $wait);
$executor->listen();
$vk = new VK\Client\VKApiClient();
$access_token = 'asdj4iht2i4ntokqngoiqn3ripogqr';
$group_id = 159895463;
$timestamp = 12;
$wait = 25;
$executor = new VK\CallbackApi\LongPoll\VKCallbackApiLongPollExecutor($vk, $access_token, $group_id, $handler, $wait);
$executor->listen($timestamp);
class ServerHandler extends VK\CallbackApi\Server\VKCallbackApiServerHandler {
const SECRET = 'ab12aba';
const GROUP_ID = 123999;
const CONFIRMATION_TOKEN = 'e67anm1';
function confirmation(int $group_id, ?string $secret) {
if ($secret === static::SECRET && $group_id === static::GROUP_ID) {
echo static::CONFIRMATION_TOKEN;
}
}
public function messageNew(int $group_id, ?string $secret, array $object) {
echo 'ok';
}
}
$handler = new ServerHandler();
$data = json_decode(file_get_contents('php://input'));
$handler->parse($data);