Download the PHP package vkcom/vk-php-sdk without Composer
On this page you can find all versions of the php package vkcom/vk-php-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vkcom/vk-php-sdk
More information about vkcom/vk-php-sdk
Files in vkcom/vk-php-sdk
Package vk-php-sdk
Short Description VK PHP SDK
License MIT
Homepage https://github.com/VKCOM/vk-php-sdk
Informations about the package 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.
This library has been created using the VK API JSON Schema. It can be found here. It uses VK API version 5.131
1. Prerequisites
- PHP 8.1
2. Installation
The VK PHP SDK can be installed using Composer by running the following command:
3. Initialization
Create VKApiClient object using the following code:
Also you can initialize VKApiClient
with different API version and different language like this:
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 before you start.
4.1. 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 and manual community access) by redirecting the user to the authorization page using the following method:
Create VKOAuth
object first:
4.1.1. For getting user access key use following command:
4.1.2. Or if you want to get community access key use:
User access key and community access key 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:
Then use this method to get the access token:
The redirect_uri should be the URL that was used to get a code at the first step.
4.2. Implicit flow
In difference with authorization code flow this flow gives you temporary access key.
Read more about user access key and community access key.
First step to get access using Implicit flow is creating VKOauth
object:
4.2.1. For getting user access key use following command:
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:
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:
And for community access key:
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.
accesstokenXXXXXX is community access token where XXXXXX is community identifier.
5. API Requests
You can find the full list of VK API methods here.
5.1. Request sample
Example of calling method users.get:
5.2. Uploading Photos into a Private Message
Please read the full manual before the start.
Call photos.getMessagesUploadServer to receive an upload address:
Then use upload() method to send files to the upload_url address received in the previous step:
You will get a JSON object with server, photo, hash fields. To save a photo call photos.saveMessagesPhoto with these three parameters:
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
Please read the full manual before the start.
Call video.save to get a video upload server address:
Send a file to upload_url received previously calling upload() method:
Videos are processed for some time after uploading.
6. Groups updates
6.1. Long Poll
Enable Long Poll for your group and specify which events should be tracked by calling the following API method:
Override methods from VK\CallbackApi\VKCallbackApiHandler class for handling events:
To start listening to LongPoll events, create an instance of your CallbackApiMyHandler class, instance of VK\CallbackApi\LongPoll\VKCallbackApiLongPollExecutor class and call method 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:
6.2. 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.
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:
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.