Skip to content

Instantly share code, notes, and snippets.

@Septdir
Created March 2, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Septdir/967188f9445515bf4512fdbaa237198f to your computer and use it in GitHub Desktop.
Save Septdir/967188f9445515bf4512fdbaa237198f to your computer and use it in GitHub Desktop.
VK Widget Comments Count
function vkCommentsCount() {
$($('[data-vkcomments-cout]')).each(function () {
var element = $(this);
var url = element.data('vkcomments-cout');
if (url == '') {
var url = location.href;
}
var data = {};
data.widget_api_id = 6187339; // id приложения
data.url = url;
data.v = '5.18'
$.ajax({
type: 'POST',
dataType: 'jsonp',
url: 'https://api.vk.com/method/widgets.getComments',
data: data,
beforeSend: function () {
element.html('0');
},
success: function (success) {
if (success.response.count > 0) {
element.html(success.response.count);
}
},
error: function (response) {
console.log(response);
}
});
});
}
$(document).ready(function () {
vkCommentsCount();
});
<?php
function getVKcommentsCount($url = '')
{
if (empty($url))
{
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
if ($_SERVER['HTTPS'] == 'on')
{
$url = 'https://' . $url;
}
else
{
$url = 'http://' . $url;
}
$widget_api_id = 0000000;// Widget app id
$request = 'https://api.vk.com/method/widgets.getComments?widget_api_id=' . $widget_api_id . '&url=' . $url . '&v=5.18';
$response = json_decode(file_get_contents($request))->response;
$result = 0;
if (isset($response->count))
{
$result = $response->count;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment