Simple Vkontakte PHP SDK
Install library with composer
dependency manager
- Add
"bocharsky-bw/vkontakte-php-sdk": "dev-master"
into therequire
section of yourcomposer.json
file - Run
$ composer.phar install
Require composer
autoloader in your index file
<?php
require __DIR__ . '/path/to/vendor/autoload.php';
Create instance of Vkontakte
class with your own configuration parameters
<?php
use \BW\Vkontakte as Vk;
$vk = new Vk([
'client_id' => 'APP_ID',
'client_secret' => 'APP_SECRET',
'redirect_uri' => 'REDIRECT_URI',
]);
Build authorization link in your template
<a href="<?php print $vk->getLoginUrl() ?>">Sign In</a>
Handle response, received from oauth.vk.com
and store access token to session
for restore it when page will be reload
<?php
session_start(); // start session if you don't
if (isset($_GET['code'])) {
$vk->authenticate();
$_SESSION['access_token'] = $vk->getAccessToken();
header('Location: ' . $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
exit;
} else {
$vk->setAccessToken($_SESSION['access_token']);
var_dump($_SESSION);
}
Get the authorized user ID
<?php
$userId = $vk->getUserId();
var_dump($userId);
<?php
$users = $vk->api('users.get', [
'user_id' => '1',
'fields' => [
'photo_50',
'city',
'sex',
],
]);
print_r($users);
For more info read the official docs: