Hi, Habr!
At one time, surfing the Internet for rational use of the Vkontakte API, I could not find anything intelligible, the only libraries that I found were implemented without using any standard practices and without beautiful code. I decided to correct the existing misunderstanding and wrote my library to work with the Vkontakte API.
Burning details and approaches under habrakat.
It so happened that API Vk is implemented quite well, except for some non-logical points, which I will mention later. But speech, today is not about quality, but about a specific application.
')
Immediately it is necessary to make a reservation, in addition to the description, I will bring pieces of working code to my library, a link to which I will give at the end of the article. The library works on the latest stable version 5.5, if you cut the generators from the batch receive, it should work on 5.4.
Let's start with how we can pass authorization.
In terms of obtaining an access key (Access Token), we can do it in
three ways :
- Server authorization (so-called site authorization)
- Client Authorization (Standalone)
- Application Server Authorization
The most interesting for the developer, represent the first two. The first one allows you to authorize the user on the site and get his access key, the second one will allow you to authorize your application, for example, Dekstop or Mobile. Looking ahead, the second option gives us tremendous opportunities, and the first, only a small part of them.
The algorithm for obtaining in the first case is reduced to the following points:
- We display a link to authorize the user, which we format according to the documentation.
- The user goes through it and logs in
- Users are redirected to our application's REDIRECT_URI with the GET parameter code
- Our application must execute a request to the API containing the code to get the user access key.
- The API responds, either with an object containing an access key, or with an error.
Sample code with which you can turn this is not a tricky thing.
$auth = getjump\Vk\Auth::getInstance(); $auth->setAppId('3470411')->setScope('SCOPE')->setSecret('SECRET CODE')->setRedirectUri('http://localhost/test.php'); $token=$auth->startCallback(); printf("<a href='%s' target='_top'>LINK</a>", $auth->getUrl());
It is assumed that our domain is localhost, and the current file is test.php. If everything went well, then our $ token variable will contain the user access key that passed authorization.
From the moment we have the access key, we can perform
requests to the API . The general query logic is simple, you pass a specially crafted request to the URL API. The request must contain the name of the method and the arguments.
api.vk.com/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN
The list of methods is one of the rich things API. In it, you can find methods that do not require an access key for their work, therefore you can call them without receiving it.
When using the library, we need to create a base object, for example:
$vk = getjump\Vk\Core::getInstance()->apiVersion('5.5')->setToken($token);
A couple of examples of queries using the library:
Through an anonymous function in each, exactly 100 objects will pass that contain data about users from 1 to 100. Notice, if we remove the function call, then there will be no request, all because the object that has the __call and __get magic methods is returned, allows us to make a request when we really need it.
$vk->request('users.get', ['user_ids' => range(1, 100)])->each(function($i, $v) { if($v->last_name == '') return; print $v->last_name . '<br>'; });
One of the things that opens us to us is the use of generators for batch receiving. That is, we only receive data when we need it. The following example will allow us to receive ALL of our messages, with requests of 100. Be careful, the
method requires that you have rights for the messages, Standalone applications, the same authorization and, accordingly, the transfer of the access key.
foreach($vk->request('messages.get')->batch(100) as $data) { $data->each(function($i, $m) { if(isset($m->body)) print $m->body . PHP_EOL; }); }
A good method that can be found in the API is
execute . It takes the code parameter as an argument, code - some pseudo JavaScript, which allows us to execute our code on the server side, it also allows us to execute stored procedures that we can create when editing our application.
I could not ignore this thing and implemented it in the library. In a nutshell, it allows you to perform several queries as one. See the following sample code.
$js1 = $vk->request('messages.get', ['count' => 200, 'offset' =>0 * 200])->toJs();
As promised, one of those misunderstandings that you may encounter in the current API version (5.21), the
users.get method will return a response to us, like an array, although in other places, for example
friends.get , starting with version number 5, we are returned count and items fields, it seems to me not quite logical, besides, it requires extra code when working with the API.
Also, in the library, handlers are implemented for some operations, with your help they can become more.
With the use of the library, we can achieve quite a nice and beautiful code, and this is the most important thing in our case is not easy.
It is likely that there were some misunderstandings or bugs in the code, I hope for your attention and Pull Requests are welcome.
The library mostly meets the PSR-0 standard.
I hope I managed to show you that API Vkontakte is not scary, but even pleasant.
Thank you for attention!
Link to github:
github.com/getjump/VkApiPHPComprehensive API
Guide :
vk.com/dev/mainPSR-0:
github.com/getjump/fig-standards/blob/master/accepted/PSR-0.mdPackagist:
packagist.org/packages/getjump/vkUPDATE:
You can install via Composer:
composer require "getjump/vk:*"
UPDATE 2:
Now the minimum version is 5.4, theoretically (in theory, most of its functionality is not used) may start at 5.3 if you do not use the short syntax for arrays.