Skip to content

Instantly share code, notes, and snippets.

@d9k
Last active February 21, 2018 12:39
Show Gist options
  • Save d9k/c17592d5ef054e634c7dbe48be0c186f to your computer and use it in GitHub Desktop.
Save d9k/c17592d5ef054e634c7dbe48be0c186f to your computer and use it in GitHub Desktop.
<?php
# Library https://github.com/mibe/FeedWriter was used
# php 5.3 required!
require_once 'FeedWriter_Lib//Item.php';
require_once 'FeedWriter_Lib//Feed.php';
require_once 'FeedWriter_Lib//RSS2.php';
require_once 'FeedWriter_Lib//InvalidOperationException.php';
$FEED_URL = 'http://pskov-eparhiya.ru/api/getNews';
$MAX_DESCRIPTION_LENGTH = 400;
// suppress possible errors
$dataArticles = @json_decode(file_get_contents($FEED_URL), 1);
if (!$dataArticles) {
// if error on load or parse then empty data
$dataArticles = ['test'];
}
// for debug:
//var_export($dataArticles);
date_default_timezone_set('UTC');
use \FeedWriter\RSS2;
//Creating an instance of RSS2 class.
$rssFeed = new RSS2();
//Setting the channel elements
//Use wrapper functions for common channel elements
$rssFeed->setTitle('pskov-eparhiya.ru RSS feed');
$rssFeed->setLink('http://pskov-eparhiya.ru/');
$rssFeed->setDescription('Официальный сайт Псковского Епархиального Управления RSS');
function mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) {
$lines = explode($break, $str);
foreach ($lines as &$line) {
$line = rtrim($line);
if (mb_strlen($line) <= $width)
continue;
$words = explode(' ', $line);
$line = '';
$actual = '';
foreach ($words as $word) {
if (mb_strlen($actual.$word) <= $width)
$actual .= $word.' ';
else {
if ($actual != '')
$line .= rtrim($actual).$break;
$actual = $word;
if ($cut) {
while (mb_strlen($actual) > $width) {
$line .= mb_substr($actual, 0, $width).$break;
$actual = mb_substr($actual, $width);
}
}
$actual .= ' ';
}
}
$line .= trim($actual);
}
return implode($break, $lines);
}
function string_cut_to_max_length($s, $maxLength) {
if (mb_strlen($s) > $maxLength - 3) {
// return mb_substr($s, 0, $maxLength - 3) . '...';
$result = mb_wordwrap($s, $maxLength - 3);
$result = substr($result, 0, strpos($result, "\n"));
return $result . '...';
} else {
return $s;
}
}
foreach ($dataArticles as $article) {
$rssItem = $rssFeed->createNewItem();
//Add item details
$rssItem->setTitle($article['title']);
$rssItem->setLink($article['link']);
if (isset($article['created_at'])) {
// ожидается дата вида "2018-02-01"
//DateTime::createFromFormat('Y-m-d H:i:s', $mysql_source_date);
//$dateTime = DateTime::createFromFormat('Y-m-d H:i', $article['created_at'].' 00:00');
$rssItem->setDate(strtotime($article['created_at']));
//$rssItem->setDate($dateTime);
}
$imageUrl = (isset($article['img'])) ? $article['img'] : null;
$description = ($imageUrl ? '<img src="'.$imageUrl.'" />' : '')
. '<p>'.string_cut_to_max_length($article['text'], $MAX_DESCRIPTION_LENGTH).'</p>';
$rssItem->setDescription($description);
//$rssItem->setContent($article['text']); //error: supported in ATOM feeds only
$rssItem->setId($article['id']);
$rssFeed->addItem($rssItem);
}
$rssFeed->printFeed();
// test string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment