Skip to content

Instantly share code, notes, and snippets.

@Neolot
Created July 13, 2012 16:12
Show Gist options
  • Select an option

  • Save Neolot/3105735 to your computer and use it in GitHub Desktop.

Select an option

Save Neolot/3105735 to your computer and use it in GitHub Desktop.
WORDPRESS Счетчик FeedBurner с кэшированием
<?php
// Счетчик FeedBurner с кэшированием
function nlt_getFeedCounter($feedid='androidbar') {
$output = get_transient('rss_counter'); // Получаем данные из кэша
if ( $output === false || $output == '' ){ // Если кэш сброшен или пустой, то получаем данные
$twodayago = date('Y-m-d', strtotime('-2 days', time()));
$onedayago = date('Y-m-d', strtotime('-1 days', time()));
$today = date('Y-m-d');
$api = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=$feedid&dates=$twodayago,$onedayago";
// Инициализация curl-сессии
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $api);
$data = curl_exec($ch);
$base_code = curl_getinfo($ch);
curl_close($ch);
if ($base_code['http_code']=='401'){
$burner_count_circulation = 'Доступ к Awareness API запрещен';
$burner_date = $today;
} else {
$xml = new SimpleXMLElement($data); // Получаем данные
$bis = $xml->attributes(); // Если счетчик равен нулю, то выводим данные за предыдущие дни
if ($bis=='ok'){
foreach ($xml->feed as $feed) {
if ($feed->entry[1]['circulation']=='0'){
$burner_count_circulation = $feed->entry[0]['circulation'];
$burner_date = $feed->entry[0]['date'];
} else {
$burner_count_circulation = $feed->entry[1]['circulation'];
$burner_date = $feed->entry[1]['date'];
}
}
}
if ($bis=='fail'){
switch ($xml->err['code']) {
case 1:
$burner_count_circulation = 'Лента не найдена';
break;
case 5:
$burner_count_circulation = 'Отсутствует требуемый параметр (URI)';
break;
case 6:
$burner_count_circulation = 'Неверный формат параметра (DATES)';
break;
}
$burner_date = $today;
}
}
$output = $burner_count_circulation;
set_transient( 'rss_counter', $output, 60*10 ); // Пишем в кэш
}
echo $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment