- Posts: 8
COMMUNITY FORUM
Custom K2 RSS with JSON data decode
- Valentine Solo
- Topic Author
- Offline
- New Member
Less
More
7 years 2 months ago #160480
by Valentine Solo
Custom K2 RSS with JSON data decode was created by Valentine Solo
Few weeks ago I had to build a custom RSS feed for external service (note: NOT W3C compliant). And I did it with K2 json output: url?format=json
Here is the code itself, in case someone will face the same task.
Hope, it will be useful
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
Less
More
- Posts: 15920
7 years 2 months ago #160482
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
Nice once :)
Kudos!
Kudos!
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Александр Павлов
- Offline
- New Member
Less
More
- Posts: 7
7 years 10 hours ago #161606
by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
Where to make changes? The name and path of the file?
Thanks in advance
Thanks in advance
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
7 years 8 hours ago #161611
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
This is a standalone file.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Александр Павлов
- Offline
- New Member
Less
More
- Posts: 7
6 years 11 months ago #161715
by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
how to use it for joomla?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
6 years 11 months ago #161726
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
Create a php file and paste the code inside it.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Александр Павлов
- Offline
- New Member
Less
More
- Posts: 7
6 years 11 months ago #161752
by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
chmod 644 ?
code line:what is the "allnews", do I need to change it?
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
Less
More
- Posts: 15920
6 years 11 months ago #161754
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
You need to change this to your actual site's JSON feed.
This would be a similar URL to your RSS feed.
This would be a similar URL to your RSS feed.
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Александр Павлов
- Offline
- New Member
Less
More
- Posts: 7
6 years 11 months ago #161755
by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
yeah, so I did, BUT check Yandex displays an empty feed
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
6 years 11 months ago #161759
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
It is not a valid feed so expect some issues.
Why aren't you using the default feed?
Why aren't you using the default feed?
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.