bocharsky-bw / vkontakte-php-sdk
Vkontakte PHP SDK
Installs: 57 854
Dependents: 2
Suggesters: 0
Security: 0
Stars: 32
Watchers: 10
Forks: 38
Open Issues: 1
Requires
- php: >=5.3.0
- ext-curl: *
- ext-json: *
README
A simple and lightweight PHP SDK library for VKontakte social network.
Install
Install library with Composer dependency manager:
$ composer require bocharsky-bw/vkontakte-php-sdk
Include
Require composer
autoloader in your index file:
require __DIR__ . '/path/to/vendor/autoload.php';
Create instance of Vkontakte
class with your own configuration parameters:
use \BW\Vkontakte as Vk; $vk = new Vk([ 'client_id' => 'APP_ID', 'client_secret' => 'APP_SECRET', 'redirect_uri' => 'REDIRECT_URI', ]);
OAuth authorization
Build authorization link in your template:
<a href="<?= $vk->getLoginUrl() ?>">Authenticate</a>
Handle response, received from oauth.vk.com
and store access token to session
for restore it when page will be reload:
session_start(); // start session if you don't if (isset($_GET['code'])) { $vk->authenticate($_GET['code']); $_SESSION['access_token'] = $vk->getAccessToken(); header('Location: '.'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); exit; } else { $accessToken = isset($_SESSION['access_token']) ? $_SESSION['access_token'] : null; $vk->setAccessToken($accessToken); var_dump($_SESSION['access_token']); }
Get the authenticated user ID
$userId = $vk->getUserId(); var_dump($userId);
Calling API
/** @var array[] $users */ $users = $vk->api('users.get', [ 'user_id' => 1, 'fields' => [ 'photo_50', 'city', 'sex', ], ]); var_dump($users);
For more info read the official docs: