Moving in step with the times, I decided to place on my site the VKontakte comment widget. It is quite enough patience, Google and documentation to achieve result. Place the widget itself is not difficult, VKontakte provides a simple interface generating code for the site. I wanted to display the number of comments in some places. Here there were difficulties, as it usually happens, the devil is in the details. I will try to sort through what the documentation is silent about.
1.Registration of the VKontakte website
To place the widget itself, you must first register the site as an application Vkontakte
>> here . We fill in the form, we receive SMS, once again we fill in the form, as a result of registering the application, we should receive
two keys :
api_id - identifier of our site as an application in VKontakte
')
api_secret is a secret key that only the owner of the application knows. It encrypts data that could otherwise be substituted / corrupted / distorted by an attacker in order to generate an electronic signature on this data on the Vkontakte side, we need it in order to check them for authenticity. And yes, we also need it in the future.
PS: Vkontakte provides a simplified way of registering an application for the Comments widget, but in this case it is impossible to get the api_secret key (at least I spent an hour looking for a way to get it and ... did not find it), which is necessary if the number of comments is stored. Therefore, do not be lazy and create an application.2. Creating a widget
We create the widget itself, I don’t have the desire to duplicate the documentation for the widget, I’ll say that in the end something like this should turn out
VK.init({apiId: %_api_id%, onlyWidgets: true}); VK.Widgets.Comments("vk_comments", {limit: 10, width: "1000", attach: "*", onChange: addCommentCallback}, "post_<?php echo $post['id']?>");
what is onChange, read on; The third parameter is the ID of this widget (block) of comments, if you do not specify it as id, the md5 hash from location.href (links to this page) is used. It didn’t fit me, as I had several different links to one element. At this stage, the widget is already working, now you need to figure out how to display the number of comments.
3.Transfer the number of comments per server
Obviously, the most successful option is to store the number of comments in the database, even if we do not store the comments themselves, since getting the number of comments through the VKontakte API will slow down the site too much. I store the number of comments in a separate column of the data table. The only question is how to keep the value in the database in an updated state and avoid desynchronization.
The comment widget API has an
onChange parameter. The function that the
onChange parameter
contains is called each time a delete / create comment event
occurs . The onChange callback function is called with four parameters.
num - number of comments
last_comment - last comment
date - date
sign - key
This is what we need. Now we write the addCommentCallback function itself, it will do the ajax request, passing all 4 parameters above, as well as the id of the record to the server. Here's what I got (using jquery):
function addCommentCallback(num, last_comment, date, sign){ $.post("%____%",{ type: 'vkontakte', num: num, last_comment: last_comment, date: date, sign: sign, id: "<?php echo $this->post['id']?>" }); }
I doubt that you want to turn your website into a fence on which you can write anything, and one day instead of the number of comments, you will find a three-letter word or an unflattering opinion about you and your website. Therefore, before we save the number of comments, we verify that the data that came from the user is valid.
4. Validation of data
Now we need the key api_secret, which we received when registering the application. Again, I personally did not find a way to get this code if I register a site through a simplified text.
The php-code processing the request, and the number of comments written to the database:
$post = $_POST; if (!isset($post['num'])){ $error = ' '; } else{ $apiSecret = %_API_SECRET%;
That's all! If you wish, you can save not only the number of comments in the database, but also the comments themselves. I hope that the article turned out to be useful and I did not waste my time.