Created
March 2, 2018 14:14
-
-
Save Septdir/967188f9445515bf4512fdbaa237198f to your computer and use it in GitHub Desktop.
VK Widget Comments Count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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