PHPackages                             corponat/vk-php-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. corponat/vk-php-sdk

ActiveLibrary[API Development](/categories/api)

corponat/vk-php-sdk
===================

VK PHP SDK

5.102.0(3y ago)014MITPHPPHP &gt;=7.1

Since Jan 9Pushed 3y agoCompare

[ Source](https://github.com/corponat/vk-php-sdk)[ Packagist](https://packagist.org/packages/corponat/vk-php-sdk)[ Docs](https://github.com/corponat/vk-php-sdk)[ RSS](/packages/corponat-vk-php-sdk/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

vk-php-sdk
==========

[](#vk-php-sdk)

PHP library for VK API interaction, includes OAuth 2.0 authorization and API methods. Full VK API features documentation can be found [here](http://vk.com/dev).

This library has been created using the VK API JSON Schema. It can be found [here](https://github.com/VKCOM/vk-api-schema). It uses VK API [version](https://vk.com/dev/versions) 5.101

[![Packagist](https://camo.githubusercontent.com/7776e2096c130f0b58a9474fed9e9c28766e7936d0f297f78730952c6a93e40a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f766b636f6d2f766b2d7068702d73646b2e737667)](https://packagist.org/packages/vkcom/vk-php-sdk)

1. Prerequisites
----------------

[](#1-prerequisites)

- PHP 7.1 or later

2. Installation
---------------

[](#2-installation)

The VK PHP SDK can be installed using Composer by running the following command:

```
composer require vkcom/vk-php-sdk
```

3. Initialization
-----------------

[](#3-initialization)

Create VKApiClient object using the following code:

```
$vk = new VK\Client\VKApiClient();
```

Also you can initialize `VKApiClient` with different API version and different language like this:

```
$vk = new VKApiClient('5.101');
```

```
$vk = new VKApiClient('5.101', VK\Client\Enums\VKLanguage::ENGLISH);
```

4. Authorization
----------------

[](#4-authorization)

The library provides the authorization flows for user based on OAuth 2.0 protocol implementation in vk.com API. Please read the full [documentation](https://vk.com/dev/access_token) before you start.

### 4.1. Authorization Code Flow

[](#41-authorization-code-flow)

OAuth 2.0 Authorization Code Flow allows calling methods from the server side.

This flow includes two steps — obtaining an authorization code and exchanging the code for an access token. Primarily you should obtain the "code" ([manual user access](https://vk.com/dev/authcode_flow_user) and [manual community access](https://vk.com/dev/authcode_flow_group)) by redirecting the user to the authorization page using the following method:

Create `VKOAuth` object first:

```
$oauth = new VK\OAuth\VKOAuth();
```

#### 4.1.1. For getting **user access key** use following command:

[](#411-for-getting-user-access-key-use-following-command)

```
$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);
```

#### 4.1.2. Or if you want to get **community access key** use:

[](#412-or-if-you-want-to-get-community-access-key-use)

```
$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);
```

[User access key](https://vk.com/dev/permissions?f=1.%20%D0%9F%D1%80%D0%B0%D0%B2%D0%B0%20%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%B0%20%D0%B4%D0%BB%D1%8F%20%D1%82%D0%BE%D0%BA%D0%B5%D0%BD%D0%B0%20%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8F) and [community access key](https://vk.com/dev/permissions?f=2.%20%D0%9F%D1%80%D0%B0%D0%B2%D0%B0%20%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%B0%20%D0%B4%D0%BB%D1%8F%20%D1%82%D0%BE%D0%BA%D0%B5%D0%BD%D0%B0%20%D1%81%D0%BE%D0%BE%D0%B1%D1%89%D0%B5%D1%81%D1%82%D0%B2%D0%B0) uses different values inside scope array

After successful authorization user's browser will be redirected to the specified **redirect\_uri**. Meanwhile the code will be sent as a GET parameter to the specified address:

```
https://example.com?code=CODE
```

Then use this method to get the access token:

```
$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'];
```

The **redirect\_uri** should be the URL that was used to get a code at the first step.

### 4.2. Implicit flow

[](#42-implicit-flow)

In difference with authorization code flow this flow gives you temporary access key.

Read more about [user access key](https://vk.com/dev/implicit_flow_user) and [community access key](https://vk.com/dev/implicit_flow_group).

First step to get access using Implicit flow is creating `VKOauth` object:

```
$oauth = new VK\OAuth\VKOAuth();
```

#### 4.2.1. For getting **user access key** use following command:

[](#421-for-getting-user-access-key-use-following-command)

```
$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);
```

If you want to make user getting access anyway, set **revoke\_auth** as true.

#### 4.2.2. Or if you want to get **community access key** use:

[](#422-or-if-you-want-to-get-community-access-key-use)

```
$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);
```

Arguments are similar with authorization code flow

After successful authorization user's browser will be redirected to the specified **redirect\_uri**. Meanwhile the access token will be sent as a fragment parameter to the specified address:

For **user access key** will be:

```
https://example.com#access_token=533bacf01e11f55b536a565b57531ad114461ae8736d6506a3&expires_in=86400&user_id=8492&state=123456
```

And for **community access key**:

```
https://example.com#access_token_XXXXXX=533bacf01e11f55b536a565b57531ad114461ae8736d6506a3&expires_in=86400
```

**access\_token** is your new access token.
**expires\_in** is lifetime of access token in seconds.
**user\_id** is user identifier.
**state** is string from `authorize` method.
access\_token\_**XXXXXX** is community access token where XXXXXX is community identifier.

5. API Requests
---------------

[](#5-api-requests)

You can find the full list of VK API methods [here](https://vk.com/dev/methods).

### 5.1. Request sample

[](#51-request-sample)

Example of calling method **users.get**:

```
$vk = new VK\Client\VKApiClient();
$response = $vk->users()->get($access_token, [
    'user_ids'  => [1, 210700286],
    'fields'    => ['city', 'photo'],
]);
```

### 5.2. Uploading Photos into a Private Message

[](#52-uploading-photos-into-a-private-message)

Please read [the full manual](https://vk.com/dev/upload_files?f=4.%20Uploading%20Photos%20into%20a%20Private%20Message) before the start.

Call **photos.getMessagesUploadServer** to receive an upload address:

```
$vk = new VK\Client\VKApiClient();
$address = $vk->photos()->getMessagesUploadServer('{access_token}');
```

Then use **upload()** method to send files to the **upload\_url** address received in the previous step:

```
$vk = new VK\Client\VKApiClient();
$photo = $vk->getRequest()->upload($address['upload_url'], 'photo', 'photo.jpg');
```

You will get a JSON object with **server**, **photo**, **hash** fields. To save a photo call **photos.saveMessagesPhoto** with these three parameters:

```
$vk = new VK\Client\VKApiClient();
$response_save_photo = $vk->photos()->saveMessagesPhoto($access_token, [
    'server' => $photo['server'],
    'photo'  => $photo['photo'],
    'hash'   => $photo['hash'],
]);
```

Then you can use **owner\_id** and **id** parameters from the last response to create an attachment of the uploaded photo.

### 5.3. Uploading Video Files

[](#53-uploading-video-files)

Please read [the full manual](https://vk.com/dev/upload_files_2?f=9.%20Uploading%20Video%20Files) before the start.

Call **video.save** to get a video upload server address:

```
$vk = new VK\Client\VKApiClient();
$address = $vk->video()->save($access_token, [
    'name' => 'My video',
]);
```

Send a file to **upload\_url** received previously calling **upload()** method:

```
$vk = new VK\Client\VKApiClient();
$video = $vk->getRequest()->upload($address['upload_url'], 'video_file', 'video.mp4');
```

Videos are processed for some time after uploading.

6. Groups updates
-----------------

[](#6-groups-updates)

### 6.1. Long Poll

[](#61-long-poll)

Enable Long Poll for your group and specify which events should be tracked by calling the following API method:

```
$vk = new VK\Client\VKApiClient();
$vk->groups()->setLongPollSettings($access_token, [
  'group_id'      => 159895463,
  'enabled'       => 1,
  'message_new'   => 1,
  'wall_post_new' => 1,
]);
```

Override methods from VK\\CallbackApi\\VKCallbackApiHandler class for handling events:

```
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'];
    }
}
```

To start listening to LongPoll events, create an instance of your CallbackApiMyHandler class, instance of VK\\CallbackApi\\LongPoll\\VKCallbackApiLongPollExecutor class and call method listen():

```
$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();
```

Parameter **wait** is the waiting period.

While calling function **listen()** you can also specify the number of the event from which you want to receive data. The default value is the number of the last event.

Example:

```
$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);
```

### 6.2. Callback API

[](#62-callback-api)

CallbackApi handler will wait for event notifications form VK. Once an event has occurred, you will be notified of it and will be able to handle it. More information [here](https://vk.com/dev/callback_api).

To start using Callback API you need to configure it under the "Manage community" tab of your community page.

The first step is confirming your domain. VK sends a request to your server with the event type **confirmation** and you need to send back a confirmation string. For other types of events you need to send back `ok` string.

Take a look at this example:

```
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);
```

To handle events you need to override methods from VK\\CallbackApi\\Server\\VKCallbackApiServerHandler class as shown above.

`confirmation` event handler has 2 arguments: group id, and secret key. You need to override this method.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1215d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/30d2768aa2bf73ec4328feefcbeb79934770d149914633054b70bc07050bcbf0?d=identicon)[corponat](/maintainers/corponat)

---

Top Contributors

[![tsivarev](https://avatars.githubusercontent.com/u/1080768?v=4)](https://github.com/tsivarev "tsivarev (21 commits)")[![Mobyman](https://avatars.githubusercontent.com/u/1827440?v=4)](https://github.com/Mobyman "Mobyman (18 commits)")[![rthakohov](https://avatars.githubusercontent.com/u/11814414?v=4)](https://github.com/rthakohov "rthakohov (4 commits)")[![corponat](https://avatars.githubusercontent.com/u/39400473?v=4)](https://github.com/corponat "corponat (2 commits)")[![povargek](https://avatars.githubusercontent.com/u/9640178?v=4)](https://github.com/povargek "povargek (1 commits)")[![vkzv](https://avatars.githubusercontent.com/u/2766454?v=4)](https://github.com/vkzv "vkzv (1 commits)")

---

Tags

sdkvkcorponat

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/corponat-vk-php-sdk/health.svg)

```
[![Health](https://phpackages.com/badges/corponat-vk-php-sdk/health.svg)](https://phpackages.com/packages/corponat-vk-php-sdk)
```

###  Alternatives

[vkcom/vk-php-sdk

VK PHP SDK

213803.4k17](/packages/vkcom-vk-php-sdk)[mocking-magician/coinbase-pro-sdk

Library for coinbase pro API calls

233.2k](/packages/mocking-magician-coinbase-pro-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
