Advertisement
Neolot

Счетчик FeedBurner с кэшированием

Jan 28th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. // Счетчик FeedBurner с кэшированием
  2.  
  3. function nlt_getFeedCounter($feedid='androidbar') {
  4.     $output = get_transient('rss_counter'); // Получаем данные из кэша
  5.     if ( $output === false || $output == '' ){ // Если кэш сброшен или пустой, то получаем данные
  6.         $twodayago = date('Y-m-d', strtotime('-2 days', time()));
  7.         $onedayago = date('Y-m-d', strtotime('-1 days', time()));
  8.         $today = date('Y-m-d');
  9.  
  10.         $api = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=$feedid&dates=$twodayago,$onedayago";
  11.  
  12.         // Инициализация curl-сессии
  13.         $ch = curl_init();
  14.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15.         curl_setopt($ch, CURLOPT_URL, $api);
  16.         $data = curl_exec($ch);
  17.         $base_code = curl_getinfo($ch);
  18.         curl_close($ch);
  19.  
  20.         if ($base_code['http_code']=='401'){
  21.             $burner_count_circulation = 'Доступ к Awareness API запрещен';
  22.             $burner_date = $today;
  23.         } else {
  24.  
  25.             $xml = new SimpleXMLElement($data); // Получаем данные
  26.             $bis = $xml->attributes();  // Если счетчик равен нулю, то выводим данные за предыдущие дни
  27.  
  28.             if ($bis=='ok'){
  29.                 foreach ($xml->feed as $feed) {
  30.                     if ($feed->entry[1]['circulation']=='0'){
  31.                         $burner_count_circulation = $feed->entry[0]['circulation'];
  32.                         $burner_date  =  $feed->entry[0]['date'];
  33.                     } else {
  34.                         $burner_count_circulation = $feed->entry[1]['circulation'];
  35.                         $burner_date  =  $feed->entry[1]['date'];
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             if ($bis=='fail'){
  41.                 switch ($xml->err['code']) {
  42.                     case 1:
  43.                         $burner_count_circulation = 'Лента не найдена';
  44.                         break;
  45.                     case 5:
  46.                         $burner_count_circulation = 'Отсутствует требуемый параметр (URI)';
  47.                         break;
  48.                     case 6:
  49.                         $burner_count_circulation = 'Неверный формат параметра (DATES)';
  50.                         break;
  51.                 }
  52.                 $burner_date = $today;
  53.             }
  54.         }
  55.         $output = $burner_count_circulation;
  56.         set_transient( 'rss_counter', $output, 60*10 ); // Пишем в кэш
  57.     }
  58.     echo $output;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement