subb98 / vk-publisher
Auto-sending (crossposting) messages to VKontakte wall
2.1
2020-02-16 10:31 UTC
Requires
- php: ^7.1
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^7.2
This package is auto-updated.
Last update: 2024-11-17 05:26:30 UTC
README
PHP library for auto-sending (crossposting) messages to VKontakte wall
Features
- Send a message or attachment (or all together) to the wall
- Upload photo (external or local) to album
Requirements
- PHP 7.1 or above
- cURL extension
- JSON extension
Installation
composer require subb98/vk-publisher
Usage
Getting access_token
For one step
- Getting access_token
- Request example:
https://oauth.vk.com/authorize?client_id=1234567&display=page&redirect_uri=&scope=photos,wall,groups,offline&response_type=token&v=5.103
- Response example:
https://oauth.vk.com/blank.html#access_token=fe8c3650bdf7ad7185598c5a32c43a4e8d38bf1135e7d7920fdafd5dc5615949ba3fc7b461d6fd120b97f&expires_in=0&user_id=89012345
- Request example:
Or for two steps
-
Getting the code to get access_token
- Request example:
https://oauth.vk.com/authorize?client_id=1234567&display=page&redirect_uri=&scope=photos,wall,groups,offline&response_type=code&v=5.103
- Response example:
https://oauth.vk.com/blank.html#code=3b2a554d20bd48c360
- Request example:
-
Getting access_token
- Request example:
https://oauth.vk.com/access_token?client_id=1234567&client_secret=9Wnk7UcEpeFAHhJYyyJg&redirect_uri=&code=3b2a554d20bd48c360
- Response example:
{"access_token":"fe8c3650bdf7ad7185598c5a32c43a4e8d38bf1135e7d7920fdafd5dc5615949ba3fc7b461d6fd120b97f","expires_in":0,"user_id":89012345}
- Request example:
Note: don't use scope=offline
if you fear access_token is may be compromised.
See more: Getting access token
Sending a message to the wall
Without photo
use Subb98\VkPublisher\Models\Settings;
use Subb98\VkPublisher\Services\PostSenderService;
$settings = (new Settings())
->setOwnerId(-169116756) // you need a minus sign if it's a group
->setAccessToken('fe8c3650bdf7ad7185598c5a32c43a4e8d38bf1135e7d7920fdafd5dc5615949ba3fc7b461d6fd120b97f')
->setApiVersion('5.103');
$postSender = new PostSenderService($settings);
$postSender->sendPostToWall("Message sends by \"VK Publisher\" PHP library\n\nLorem ipsum, dolor sit amet consectetur adipisicing elit. Tenetur unde libero quidem adipisci fugiat, error facilis ducimus, veritatis id amet nemo. Mollitia corporis vel quae rerum deleniti! Ducimus, corporis optio.");
With photo(s)
use Subb98\VkPublisher\Services\PhotoUploaderService;
$settings->setAlbumId(256572563);
$photoUploader = new PhotoUploaderService($settings);
$photos[] = $photoUploader->uploadPhotoToAlbum('https://i.imgur.com/G82OZCz.jpg');
$photos[] = $photoUploader->uploadPhotoToAlbum(__DIR__ . '/img/peter-as-superman.jpg');
$postSender->sendPostToWall("Message sends by \"VK Publisher\" PHP library\n\nLorem ipsum, dolor sit amet consectetur adipisicing elit. Tenetur unde libero quidem adipisci fugiat, error facilis ducimus, veritatis id amet nemo. Mollitia corporis vel quae rerum deleniti! Ducimus, corporis optio.", $photos);