Installation:
composer require baks-dev/vk
php artisan vendor:publish --provider="BaksDev\Vk\VkServiceProvider" --tag="config"
config/vk.php.Configuration:
config/vk.php with your VK API credentials (client ID, client secret, redirect URI, etc.).v5.131).First Use Case: Fetch basic user info via the CLI or a controller:
use BaksDev\Vk\VkClient;
$client = app(VkClient::class);
$user = $client->api()->users()->get(['user_ids' => '123456789']);
dd($user);
config/vk.php: Default configuration.src/VkClient.php: Core client class.src/Exceptions/: Custom exceptions (e.g., VkApiException).tests/VkTestCase.php: Test patterns for integration.Authentication:
$authUrl = $client->oauth()->getAuthUrl(['scope' => 'wall,photos']);
// Redirect user to $authUrl, then handle callback:
$accessToken = $client->oauth()->handleCallback($request);
$client->setAccessToken($accessToken);
HasApiTokens trait if provided).API Calls:
$client->api()
->users()
->get(['fields' => 'city,country'], ['user_id' => auth()->id());
$batch = $client->batch();
$batch->add('users.get', ['user_ids' => '1,2,3']);
$batch->add('photos.get', ['owner_id' => '1', 'album_id' => 'profile']);
$results = $batch->execute();
Webhooks:
VkWebhook facade:
$webhook = app(VkWebhook::class);
$webhook->subscribe(['type' => 'message_new'], route('vk.webhook'));
Uploads:
$photo = $client->upload()->photos()->save('path/to/image.jpg', [
'album_id' => 'profile',
'group_id' => 123456789,
]);
AppServiceProvider:
$this->app->singleton(VkClient::class, function () {
return new VkClient(config('vk'));
});
VkClient to requests:
public function handle(Request $request, Closure $next) {
$request->merge(['vk' => app(VkClient::class)]);
return $next($request);
}
Vk\Events\AuthSuccess) if the package emits them.Rate Limits:
$user = Cache::remember("vk_user_{$userId}", now()->addMinutes(5), function () use ($client, $userId) {
return $client->api()->users()->get(['user_ids' => $userId]);
});
VkRateLimiter middleware if provided.Token Expiry:
try {
$response = $client->api()->users()->get(...);
} catch (\BaksDev\Vk\Exceptions\TokenExpiredException $e) {
$client->oauth()->refreshToken($refreshToken);
return $this->handle($request);
}
Locale/Encoding:
'default_locale' => 'en_US.UTF-8',
in app/config/app.php.Deprecated Methods:
Enable Logging:
Add to config/vk.php:
'debug' => env('APP_DEBUG', false),
'log' => true,
Logs will appear in storage/logs/vk.log.
HTTP Client Inspection:
Use Laravel’s tap to inspect raw responses:
$response = $client->api()->users()->get(...)->tap(function ($response) {
\Log::debug('Raw VK response:', $response->getData());
});
Custom API Methods: Extend the client to add missing methods:
namespace App\Services\Vk;
use BaksDev\Vk\VkClient;
class ExtendedVkClient extends VkClient {
public function customMethod(array $params) {
return $this->callApi('custom.method', $params);
}
}
Response Transformers: Override response handling in a service:
$client->setResponseTransformer(function ($response) {
return collect($response['response'])->map(fn ($item) => [
'id' => $item['id'],
'formatted_name' => "{$item['first_name']} {$item['last_name']}",
]);
});
Testing: Mock the client in tests:
$mockClient = Mockery::mock(VkClient::class);
$mockClient->shouldReceive('api')->andReturnSelf();
$mockClient->shouldReceive('users->get')->andReturn(['response' => [...]]);
$this->app->instance(VkClient::class, $mockClient);
.env for sensitive data:
VK_CLIENT_ID=your_id
VK_CLIENT_SECRET=your_secret
VK_REDIRECT_URI=http://your-app.com/vk/callback
config/vk.php to avoid breaking changes:
'api_version' => '5.131', // Use the latest stable version
How can I help you explore Laravel packages today?