- Posts: 8
COMMUNITY FORUM
Custom K2 RSS with JSON data decode
- Valentine Solo
-
Topic Author
- Offline
- New Member
Here is the code itself, in case someone will face the same task.
<?php
header("Content-Type: application/rss+xml; charset=UTF-8");
$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>'. "\n";
$rssfeed .= '<rss version="2.0" xmlns="http://backend.userland.com/rss2">'. "\n";
$rssfeed .= '<channel>'. "\n";
$rssfeed .= '<title>My domain</title>'. "\n";
$rssfeed .= '<link>https://mydomain.com</link>'. "\n";
$rssfeed .= '<description>My feed description.</description>'. "\n";
$url = "https://mydomain.com/allnews?format=json";
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach (array_slice($json['items'],0,25) as $item) {
// Use $field and $value here
$rssfeed .= '<item>'. "\n";
$rssfeed .= '<title><![CDATA[' . $item['title'] . ']]></title>'. "\n";
$rssfeed .= '<link>https://mydomain.com' . $item['link'] . '</link>'. "\n"; // adding domain to get the full url
$intro = html_entity_decode($item['introtext']);
$intro = str_replace(' ', '', $intro);
$intro = strip_tags($intro);
$rssfeed .= '<description><![CDATA[' . $intro . ']]></description>'. "\n";
$date = new DateTime($item['modified']);
$date->modify('+2 hours'); //my K2 returns pubdate (modified) in UTC format, so I had to add time zone difference in hours
$rssfeed .= '<pubDate>' . $date->format("D, d M Y H:i:s +0200") . '</pubDate>'. "\n";
$fulltext = html_entity_decode($item['fulltext']);
$fulltext = str_replace(' ', ' ', $fulltext);
$fulltext = strip_tags($fulltext);
$rssfeed .= '<yandex:full-text><![CDATA[ ' . $fulltext . ']]></yandex:full-text>'. "\n";
$rssfeed .= '</item>'. "\n";
}
$rssfeed .= '</channel>'. "\n";
$rssfeed .= '</rss>';
echo $rssfeed;
?>
Hope, it will be useful
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Kudos!
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
Thanks in advance
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
code line:
$url = "https://mydomain.com/allnews?format=json"
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
This would be a similar URL to your RSS feed.
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Why aren't you using the default feed?
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
Valentine Solo solved this problem.
But probably, I somehow incorrectly applied
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.
- Александр Павлов
-
- Offline
- New Member
- Posts: 7
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
-
- Offline
- Platinum Member
- Posts: 15920
Please Log in or Create an account to join the conversation.