Keyword

Custom K2 RSS with JSON data decode

  • Valentine Solo
  • Valentine Solo's Avatar Topic Author
  • Offline
  • New Member
More
8 years 3 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.
<?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('&nbsp;', '', $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('&nbsp;', ' ', $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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 3 months ago #160482 by Krikor Boghossian
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
Nice once :)
Kudos!

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 1 month 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

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 1 month ago #161611 by Krikor Boghossian
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
This is a standalone file.

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 1 month 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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 1 month ago #161726 by Krikor Boghossian
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
Create a php file and paste the code inside it.

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 1 month ago #161752 by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
chmod 644 ?

code line:
$url = "https://mydomain.com/allnews?format=json"
what is the "allnews", do I need to change it?

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 1 month ago #161754 by Krikor Boghossian
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.

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 1 month 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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 1 month ago #161759 by Krikor Boghossian
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?

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 1 month ago #161761 by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
the default channel is not valid for Yandex. He has other standards, not W3C compliant
Valentine Solo solved this problem.
But probably, I somehow incorrectly applied

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 1 month ago #161769 by Krikor Boghossian
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
I wish I could assist you but I do not know the specifics of Yandex.

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 4 weeks ago #161828 by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
how can I contact Valentine Solo?

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 3 weeks ago #161862 by Krikor Boghossian
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
You can look him up on social media.

Please Log in or Create an account to join the conversation.

  • Александр Павлов
  • Александр Павлов's Avatar
  • Offline
  • New Member
More
8 years 3 weeks ago #161929 by Александр Павлов
Replied by Александр Павлов on topic Custom K2 RSS with JSON data decode
I modified the file. Check Yandex is successful
a-medianews.ru/isonfeed.php?format=json
Thank you

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 3 weeks ago #161938 by Krikor Boghossian
Replied by Krikor Boghossian on topic Custom K2 RSS with JSON data decode
Nice to hear that you sorted this out.

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum