E
E
Evgeny Privalov2013-11-20 14:36:46
PHP
Evgeny Privalov, 2013-11-20 14:36:46

How to write a function in PHP that implements RSS on a website?

Good afternoon.
Can you please tell me the algorithm how to write a function in PHP that implements RSS on the site?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Boliev, 2013-11-20
@eprivalov1

Try UniversalFeedCreator
Code from documentation:

$rss = new UniversalFeedCreator();
$rss->useCached(); // use cached version if age < 1 hour
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";

//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;

$rss->link = "<a href="http://www.dailyphp.net/news">http://www.dailyphp.net/news</a>";
$rss->syndicationURL = "<a href="http://www.dailyphp.net/">http://www.dailyphp.net/</a>" . $_SERVER["PHP_SELF"];

$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "<a href="http://www.dailyphp.net/images/logo.gif">http://www.dailyphp.net/images/logo.gif</a>";
$image->link = "<a href="http://www.dailyphp.net">http://www.dailyphp.net</a>";
$image->description = "Feed provided by dailyphp.net. Click to visit.";

//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;

$rss->image = $image;

// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
    $item = new FeedItem();
    $item->title = $data->title;
    $item->link = $data->url;
    $item->description = $data->short;

    //optional
    $item->descriptionTruncSize = 500;
    $item->descriptionHtmlSyndicated = true;

    $item->date = $data->newsdate;
    $item->source = "<a href="http://www.dailyphp.net">http://www.dailyphp.net</a>";
    $item->author = "John Doe";

    $rss->addItem($item);
}

echo $rss->saveFeed("RSS1.0", "news/feed.xml");

S
Sergey, 2013-11-20
@butteff

I'll just leave it here

<?
$url = 'rss.xml';       //адрес RSS ленты

$rss = simplexml_load_file($url);       //Интерпретирует XML-файл в объект

//цикл для обхода всей RSS ленты
foreach ($rss->channel->item as $item) {
        echo '<h1>'.$item->title.'</h1>';       //выводим на печать заголовок статьи 
        echo $item->description;        //выводим на печать текст статьи
}
?>

A
Alexander Zelenin, 2013-11-20
@zelenin

https://github.com/zelenin/RSSGen

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question