Installation
composer require ailove-dev/vk-php-sdk
Verify the package loads in composer.json under require.
First Use Case: Authentication Initialize the SDK with your VK app credentials:
use AiloveDev\VkPhpSdk\VkClient;
$client = new VkClient(
'YOUR_APP_ID',
'YOUR_SECRET_KEY',
'YOUR_CALLBACK_URL'
);
Store these values in .env for security:
VK_APP_ID=your_app_id
VK_SECRET_KEY=your_secret_key
VK_CALLBACK_URL=https://your-app.com/vk/callback
First API Call (User Data) Fetch authenticated user data:
$user = $client->api()->users()->get(['user_ids' => 'me']);
dd($user);
Ensure you’ve authorized the user first.
Redirect to VK for Auth
$authUrl = $client->auth()->getAuthorizationUrl();
return redirect($authUrl);
Store the state parameter in the session for CSRF protection.
Handle Callback
$accessToken = $client->auth()->handleCallback(request());
session(['vk_access_token' => $accessToken]);
Use Token for API Calls
$client->setAccessToken(session('vk_access_token'));
$friends = $client->api()->friends()->get(['count' => 10]);
Laravel Service Provider Bind the client to the container for dependency injection:
$this->app->singleton(VkClient::class, function ($app) {
return new VkClient(
config('services.vk.app_id'),
config('services.vk.secret_key'),
config('services.vk.callback_url')
);
});
API Rate Limiting VK imposes rate limits. Cache responses:
$posts = Cache::remember('vk_wall_posts', now()->addHours(1), function () {
return $client->api()->wall()->get(['owner_id' => '-me']);
});
Webhooks Use VK’s webhooks for real-time updates:
$longPollServer = $client->api()->groups()->getLongPollServer(['group_id' => 'YOUR_GROUP_ID']);
Token Expiry
try {
$client->api()->users()->get(['user_ids' => 'me']);
} catch (\AiloveDev\VkPhpSdk\Exceptions\TokenExpiredException $e) {
$newToken = $client->auth()->refreshToken(session('vk_refresh_token'));
session(['vk_access_token' => $newToken]);
}
refresh_token securely during initial auth.Scope Mismatch
scope parameter in getAuthorizationUrl() matches the permissions you request (e.g., friends, wall). Missing scopes will cause silent failures.Deprecated Methods
execute method for undocumented endpoints).$client->setDebug(true); // Logs requests/responses to storage/logs/vk.log
response key in the result array for errors:
$result = $client->api()->users()->get(['user_ids' => 'invalid_id']);
if (isset($result['error'])) {
throw new \Exception($result['error']['error_msg']);
}
Custom API Methods Extend the SDK for undocumented endpoints:
$client->api()->execute([
'code' => 'YOUR_CODE',
'method' => 'undocumented.method',
'params' => ['param1' => 'value']
]);
Middleware for Auth Create middleware to verify VK tokens on protected routes:
public function handle($request, Closure $next) {
if (!$request->session()->has('vk_access_token')) {
return redirect()->route('vk.auth');
}
return $next($request);
}
Batch Requests
Use execute for multiple API calls in one request to reduce latency:
$client->api()->execute([
'code' => 42,
'method' => 'users.get',
'params' => ['user_ids' => 'me'],
'method2' => 'friends.get',
'params2' => ['user_id' => 'me']
]);
How can I help you explore Laravel packages today?