Hello. My name is Max. I am a PHP backend developer. One of my deepest desires has always been to create a public VKontakte. I saw how much time it took from my friends and acquaintances. Preparation of posts, planning records killed a heap and a small cart of free time that could be spent on something more useful or pleasant (or both).
The question is, and why do I generally need a public, I propose to leave it behind, everyone has different reasons for this. Well, we get down to the essence of this post.
First we need:
And so, first of all we need to get a token, which we will use when calling the methods of the VK API. To do this, go to the VKontakte section for developers . In the top menu, select the item "My Applications".
Click on the button "Create an application."
Name can put any that you like. Platform choose "Standalone-application". Click "Connect application".
The next step we need to get, in fact, the token itself.
To do this, copy the following link into the address bar of the browser, pre-filled with data.
https://oauth.vk.com/authorize?client_id=<id___(___)>&scope=photo,wall,offline&redirect_uri=https://oauth.vk.com/blank.html&response_type=token
You can add offline to the scope parameter. In fact, the token will live forever (or until the password is changed from your VKontakte account).
After you execute this request, in the address bar you will see the “access_token” parameter, which we need. Save it and do not remove it so far.
Now it's time, finally, to start building a system. We will write this whole thing on symfony 4.
Install symfony in the current folder.
composer create-project symfony/skeleton .
Also install the maker-bundle , which will allow you to generate console commands in "one click".
composer require symfony/maker-bundle --dev
And install the official PHP SDK from VKonatkte .
composer require vkcom/vk-php-sdk
Now we will generate the first console command.
php bin/console make:command
We will be asked what we want to call it? You can just call it “app: vk-post”.
Great, now we have a ready-made console command.
The next step is to create a closed album in your VK account. For what it is needed - I will tell you closer to the end of the post.
We return to our console team. First of all, we need to clear the code, removing all unnecessary from it. For example, command line arguments.
<?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; class TestCommand extends Command { protected static $defaultName = 'app:test'; protected function configure() { $this ->setDescription('VK Automation') ; } protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); // . $io->success('Success!'); } }
We import VK PHP SDK.
$vk = new VK\Client\VKApiClient();
Inside the execute () method we start writing the working code.
// API VK $vk = new VKApiClient('5.73');
And now we remember about our closed album. It is needed in order to store in it all the images that we will post on the wall. Therefore, pre-add 10-15 images in a closed album. For example, I have a public quote book, and I will fill the album with images with quotations accordingly.
API Vkonakte allow you to get a list of all the images from your album by calling the photos.get method, with two parameters:
The album ID can be obtained by going to it: https://vk.com/album196**5146_260977 **. The last digits after the underscore will be the album id.
Now we are writing code that would allow us to get a list of all the photos from the album.
$photos = $vk->photos()->get($access_token, [ 'owner_id' => '196******', 'album_id' => '26097****' ]);
The $ access_token variable is our token, which I asked you not to remove.
The method will return an array of all photos from your closed album. The format of the returned data in JSON can be like this.
{ "response": { "count": 8, "items": [ { "id": 456258500, "album_id": 26097****, "owner_id": 1967****, "photo_75": "https://pp.userapi.com/c849528/v849528304/df4d6/v6csVqSA3uU.jpg", "photo_130": "https://pp.userapi.com/c849528/v849528304/df4d7/eb6_DbdeU8M.jpg", "photo_604": "https://pp.userapi.com/c849528/v849528304/df4d8/4BkYfRNmWwE.jpg", "photo_807": "https://pp.userapi.com/c849528/v849528304/df4d9/P2zc6j04xt4.jpg", "width": 806, "height": 730, "text": "", "date": 1546514839 }, { "id": 456258501, "album_id": 26097****, "owner_id": 19675****, "photo_75": "https://pp.userapi.com/c845323/v845323304/16887a/1XWNUVIUX4s.jpg", "photo_130": "https://pp.userapi.com/c845323/v845323304/16887b/6O8lVeX0x0k.jpg", "photo_604": "https://pp.userapi.com/c845323/v845323304/16887c/7Evr6J_eLGM.jpg", "photo_807": "https://pp.userapi.com/c845323/v845323304/16887d/5EMMMPmj37Y.jpg", "width": 800, "height": 533, "text": "", "date": 1546514843 }, ] }
Inside the array of items will be all our photos.
Great, we got the photos, and then what? Now they need to post on the wall of the public.
This is done by calling the wall.post method.
$vk->wall()->post($access_token, [ 'owner_id' => '-7188****', // id 'attachments' => "photo19675****_" . $photos['items'][mt_rand(0, $photos['count']-1)]['id'], // photo<id_>_<id_> ]);
public id is also not difficult to get. Just add an avatar and open it: https://vk.com/********?z=photo-*718825485**_456239371%2Falbum-7188****_0%2Frev . ID highlighted in bold.
The attachmnet field must have a special format .
In our example, I choose a random photo from a closed album and attach it to the post.
The final code will look like this:
<?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; class VkPostCommand extends Command { protected static $defaultName = 'app:vk-post'; protected function configure() { $this ->setDescription('VK Automation') ; } protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $access_token = 'yourAccessToken'; $vk = new VKApiClient('5.73'); $photos = $vk->photos()->get($access_token, [ 'owner_id' => '19675****', 'album_id' => '26097****' ]); $response = $vk->wall()->post($access_token, [ 'owner_id' => '-7188****', 'attachments' => "photo19675****_" . $photos['items'][mt_rand(0, $photos['count']-1)]['id'], ]); $io->success('Success.'); } }
After we execute the code in the console.
php bin/console app:vk-post
We will see how the picture on the wall was published in our public.
You can improve the system and add the ability to randomly select the type of post, picture or text. But where to get quotes? I found at least 2 open APIs. Surely and you can find a data source to fit your needs.
Now we need to install the code on our VDS server, install the framework through composer and configure the command execution for krone.
Run this command on your server:
crontab -e
You will be asked to choose an editor, I prefer nano.
The file will open. Add to its end:
*/45 * * * * //// bin/console app:vk-post
Now it remains to observe and enjoy the work done.
Why did I use a closed album? The wall.post method requires that the image be preloaded on VK servers. This is an extra code that can be avoided by creating a closed album and filling it manually.
From now on, your public will be able to exist completely independently, and you will only have to think about its promotion.
There is a lot of fantasy here. For example, you can add several data sources for posts, or set up automation for several public tables. It is also worth making a token in a separate place.
Experiment and good luck to you!
Source: https://habr.com/ru/post/435200/
All Articles