{"id":15017580,"url":"https://github.com/vkcom/vk-php-sdk","last_synced_at":"2025-10-25T08:02:44.824Z","repository":{"id":51227115,"uuid":"120872956","full_name":"VKCOM/vk-php-sdk","owner":"VKCOM","description":"PHP library for working with VK API","archived":false,"fork":false,"pushed_at":"2025-04-14T13:51:31.000Z","size":551,"stargazers_count":209,"open_issues_count":48,"forks_count":94,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-05-11T22:03:20.007Z","etag":null,"topics":["api","php","vk"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VKCOM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-09T07:33:01.000Z","updated_at":"2025-05-07T12:25:24.000Z","dependencies_parsed_at":"2024-06-18T11:14:51.307Z","dependency_job_id":"2fb45f55-14cd-43f9-a01e-aa0ba15fa5b3","html_url":"https://github.com/VKCOM/vk-php-sdk","commit_stats":{"total_commits":68,"total_committers":12,"mean_commits":5.666666666666667,"dds":0.5294117647058824,"last_synced_commit":"72c742391c0acbcfd993e92ca2c60e442eff8586"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VKCOM%2Fvk-php-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VKCOM%2Fvk-php-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VKCOM%2Fvk-php-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VKCOM%2Fvk-php-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VKCOM","download_url":"https://codeload.github.com/VKCOM/vk-php-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254219374,"owners_count":22034397,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","php","vk"],"created_at":"2024-09-24T19:50:42.574Z","updated_at":"2025-10-25T08:02:44.818Z","avatar_url":"https://github.com/VKCOM.png","language":"PHP","readme":"# vkcom/vk-php-sdk\n\nPHP library for VK API interaction, includes OAuth 2.0 authorization and API methods. Full VK API features documentation\ncan be found [here](http://vk.com/dev).\n\nThis library has been created using the VK API JSON Schema. It can be\nfound [here](https://github.com/VKCOM/vk-api-schema). It uses VK API [version](https://vk.com/dev/versions) 5.199\n\n[![Packagist](https://img.shields.io/packagist/v/vkcom/vkcom/vk-php-sdk.svg)](https://packagist.org/packages/vkcom/vkcom/vk-php-sdk)\n\n## 1. Prerequisites\n\n* PHP 7 or PHP 8\n\n## 2. Installation\n\nThe VK PHP SDK can be installed using Composer by running the following command:\n\n```sh\ncomposer require vkcom/vk-php-sdk\n```\n\n## 3. Initialization\n\nCreate VKApiClient object using the following code:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n```\n\nAlso you can initialize `VKApiClient` with different API version and different language like this:\n\n```php\n$vk = new VKApiClient('5.199');\n```\n\n```php\n$vk = new VKApiClient('5.199', \\VK\\Client\\Enums\\VKLanguage::ENGLISH);\n```\n\n## 4. Authorization\n\nThe library provides the authorization flows for user based on OAuth 2.0 protocol implementation in vk.com API. Please\nread the full [documentation](https://vk.com/dev/access_token) before you start.\n\n### 4.1. Authorization Code Flow\n\nOAuth 2.0 Authorization Code Flow allows calling methods from the server side.\n\nThis flow includes two steps — obtaining an authorization code and exchanging the code for an access token. Primarily\nyou should obtain the \"code\" ([manual user access](https://vk.com/dev/authcode_flow_user)\nand [manual community access](https://vk.com/dev/authcode_flow_group)) by redirecting the user to the authorization page\nusing the following method:\n\nCreate `VKOAuth` object first:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n```\n\n#### 4.1.1. For getting **user access key** use following command:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n$client_id = 1234567;\n$redirect_uri = 'https://example.com/vk';\n$display = \\VK\\OAuth\\VKOAuthDisplay::PAGE;\n$scope = [\\VK\\OAuth\\Scopes\\VKOAuthUserScope::WALL, \\VK\\OAuth\\Scopes\\VKOAuthUserScope::GROUPS];\n$state = 'secret_state_code';\n\n$browser_url = $oauth-\u003egetAuthorizeUrl(\\VK\\OAuth\\VKOAuthResponseType::CODE, $client_id, $redirect_uri, $display, $scope, $state);\n```\n\n#### 4.1.2. Or if you want to get **community access key** use:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n$client_id = 1234567;\n$redirect_uri = 'https://example.com/vk';\n$display = \\VK\\OAuth\\VKOAuthDisplay::PAGE;\n$scope = [\\VK\\OAuth\\Scopes\\VKOAuthGroupScope::MESSAGES];\n$state = 'secret_state_code';\n$groups_ids = [1, 2];\n\n$browser_url = $oauth-\u003egetAuthorizeUrl(\\VK\\OAuth\\VKOAuthResponseType::CODE, $client_id, $redirect_uri, $display, $scope, $state, $groups_ids);\n```\n\n[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)\nand [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)\nuses different values inside scope array\n\nAfter successful authorization user's browser will be redirected to the specified **redirect_uri**. Meanwhile the code\nwill be sent as a GET parameter to the specified address:\n\n```sh\nhttps://example.com?code=CODE\n```\n\nThen use this method to get the access token:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n$client_id = 1234567;\n$client_secret = 'SDAScasd'\n$redirect_uri = 'https://example.com/vk';\n$code = 'CODE';\n\n$response = $oauth-\u003egetAccessToken($client_id, $client_secret, $redirect_uri, $code);\n$access_token = $response['access_token'];\n```\n\nThe **redirect_uri** should be the URL that was used to get a code at the first step.\n\n### 4.2. Implicit flow\n\nIn difference with authorization code flow this flow gives you temporary access key.\n\nRead more about [user access key](https://vk.com/dev/implicit_flow_user)\nand [community access key](https://vk.com/dev/implicit_flow_group).\n\nFirst step to get access using Implicit flow is creating `VKOauth` object:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n```\n\n#### 4.2.1. For getting **user access key** use following command:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n$client_id = 1234567;\n$redirect_uri = 'https://example.com/vk';\n$display = \\VK\\OAuth\\VKOAuthDisplay::PAGE;\n$scope = [\\VK\\OAuth\\Scopes\\VKOAuthUserScope::WALL, \\VK\\OAuth\\Scopes\\VKOAuthUserScope::GROUPS];\n$state = 'secret_state_code';\n$revoke_auth = true;\n\n$browser_url = $oauth-\u003egetAuthorizeUrl(\\VK\\OAuth\\VKOAuthResponseType::TOKEN, $client_id, $redirect_uri, $display, $scope, $state, null, $revoke_auth);\n```\n\nIf you want to make user getting access anyway, set **revoke_auth** as true.\n\n#### 4.2.2. Or if you want to get **community access key** use:\n\n```php\n$oauth = new \\VK\\OAuth\\VKOAuth();\n$client_id = 1234567;\n$redirect_uri = 'https://example.com/vk';\n$display = \\VK\\OAuth\\VKOAuthDisplay::PAGE;\n$scope = [\\VK\\OAuth\\Scopes\\VKOAuthGroupScope::MESSAGES];\n$state = 'secret_state_code';\n$groups_ids = [1, 2];\n\n$browser_url = $oauth-\u003egetAuthorizeUrl(\\VK\\OAuth\\VKOAuthResponseType::TOKEN, $client_id, $redirect_uri, $display, $scope, $state, $groups_ids);\n```\n\nArguments are similar with authorization code flow\n\nAfter successful authorization user's browser will be redirected to the specified **redirect_uri**. Meanwhile the access\ntoken will be sent as a fragment parameter to the specified address:\n\nFor **user access key** will be:\n\n```sh\nhttps://example.com#access_token=533bacf01e11f55b536a565b57531ad114461ae8736d6506a3\u0026expires_in=86400\u0026user_id=8492\u0026state=123456\n```\n\nAnd for **community access key**:\n\n```sh\nhttps://example.com#access_token_XXXXXX=533bacf01e11f55b536a565b57531ad114461ae8736d6506a3\u0026expires_in=86400\n```\n\n**access_token** is your new access token.  \n**expires_in** is lifetime of access token in seconds.  \n**user_id** is user identifier.  \n**state** is string from `authorize` method.  \naccess_token_**XXXXXX** is community access token where XXXXXX is community identifier.\n\n## 5. API Requests\n\nYou can find the full list of VK API methods [here](https://vk.com/dev/methods).\n\n### 5.1. Request sample\n\nExample of calling method **users.get**:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$response = $vk-\u003eusers()-\u003eget($access_token, [\n    'user_ids'  =\u003e [1, 210700286],\n    'fields'    =\u003e ['city', 'photo'],\n]);\n```\n\n### 5.2. Uploading Photos into a Private Message\n\nPlease read [the full manual](https://vk.com/dev/upload_files?f=4.%20Uploading%20Photos%20into%20a%20Private%20Message)\nbefore the start.\n\nCall **photos.getMessagesUploadServer** to receive an upload address:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$address = $vk-\u003ephotos()-\u003egetMessagesUploadServer('{access_token}');\n```\n\nThen use **upload()** method to send files to the **upload_url** address received in the previous step:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$photo = $vk-\u003egetRequest()-\u003eupload($address['upload_url'], 'photo', 'photo.jpg');\n```\n\nYou will get a JSON object with **server**, **photo**, **hash** fields. To save a photo call **photos.saveMessagesPhoto\n** with these three parameters:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$response_save_photo = $vk-\u003ephotos()-\u003esaveMessagesPhoto($access_token, [\n    'server' =\u003e $photo['server'],\n    'photo'  =\u003e $photo['photo'],\n    'hash'   =\u003e $photo['hash'],\n]);\n```\n\nThen you can use **owner_id** and **id** parameters from the last response to create an attachment of the uploaded\nphoto.\n\n### 5.3. Uploading Video Files\n\nPlease read [the full manual](https://vk.com/dev/upload_files_2?f=9.%20Uploading%20Video%20Files) before the start.\n\nCall **video.save** to get a video upload server address:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$address = $vk-\u003evideo()-\u003esave($access_token, [\n    'name' =\u003e 'My video',\n]);\n```\n\nSend a file to **upload_url** received previously calling **upload()** method:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$video = $vk-\u003egetRequest()-\u003eupload($address['upload_url'], 'video_file', 'video.mp4');\n```\n\nVideos are processed for some time after uploading.\n\n## 6. Groups updates\n\n### 6.1. Long Poll\n\nEnable Long Poll for your group and specify which events should be tracked by calling the following API method:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$vk-\u003egroups()-\u003esetLongPollSettings($access_token, [\n  'group_id'      =\u003e 159895463,\n  'enabled'       =\u003e 1,\n  'message_new'   =\u003e 1,\n  'wall_post_new' =\u003e 1,\n]);\n```\n\nOverride methods from VK\\CallbackApi\\VKCallbackApiHandler class for handling events:\n\n```php\nclass CallbackApiMyHandler extends VK\\CallbackApi\\VKCallbackApiHandler {\n    public function messageNew($object) {\n        echo 'New message: ' . $object['body'];\n    }\n    \n    public function wallPostNew($object) {\n        echo 'New wall post: ' . $object['text'];\n    }\n}\n```\n\nTo start listening to LongPoll events, create an instance of your CallbackApiMyHandler class, instance of\nVK\\CallbackApi\\LongPoll\\VKCallbackApiLongPollExecutor class and call method listen():\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$access_token = 'asdj4iht2i4ntokqngoiqn3ripogqr';\n$group_id = 159895463;\n$wait = 25;\n\n$handler = new CallbackApiMyHandler();\n$executor = new \\VK\\CallbackApi\\VKCallbackApiLongPollExecutor($vk, $access_token, $group_id, $handler, $wait);\n$executor-\u003elisten();\n```\n\nParameter **wait** is the waiting period.\n\nWhile calling function **listen()** you can also specify the number of the event from which you want to receive data.\nThe default value is the number of the last event.\n\nExample:\n\n```php\n$vk = new \\VK\\Client\\VKApiClient();\n$access_token = 'asdj4iht2i4ntokqngoiqn3ripogqr';\n$group_id = 159895463;\n$timestamp = 12;\n$wait = 25;\n\n$executor = new \\VK\\CallbackApi\\VKCallbackApiLongPollExecutor($vk, $access_token, $group_id, $handler, $wait);\n$executor-\u003elisten($timestamp);\n```\n\n### 6.2. Callback API\n\nCallbackApi handler will wait for event notifications form VK. Once an event has occurred, you will be notified of it\nand will be able to handle it. More information [here](https://vk.com/dev/callback_api).\n\nTo start using Callback API you need to configure it under the \"Manage community\" tab of your community page.\n\nThe first step is confirming your domain. VK sends a request to your server with the event type **confirmation** and you\nneed to send back a confirmation string. For other types of events you need to send back `ok` string.\n\nTake a look at this example:\n\n```php\nclass ServerHandler extends \\VK\\CallbackApi\\VKCallbackApiServerHandler {\n    const SECRET = 'ab12aba';\n    const GROUP_ID = 123999;\n    const CONFIRMATION_TOKEN = 'e67anm1';\n\n    function confirmation(int $group_id, ?string $secret) {\n        if ($secret === static::SECRET \u0026\u0026 $group_id === static::GROUP_ID) {\n            echo static::CONFIRMATION_TOKEN;\n        }\n    }\n    \n    public function messageNew(int $group_id, ?string $secret, array $object) {\n        echo 'ok';\n    }\n}\n\n$handler = new ServerHandler();\n$data = json_decode(file_get_contents('php://input'));\n$handler-\u003eparse($data);\n```\n\nTo handle events you need to override methods from VK\\CallbackApi\\Server\\VKCallbackApiServerHandler class as shown\nabove.\n\n`confirmation` event handler has 2 arguments: group id, and secret key. You need to override this method.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvkcom%2Fvk-php-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvkcom%2Fvk-php-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvkcom%2Fvk-php-sdk/lists"}