/ mb_json2rss_v1.php · 9 months ago·

// Convert JSON feed to RSS, this script has been modified from its original example 
// to be specific. This script will allow you to convert the 
// JSON feed for the specific accounts you follow using your username or 
// this can be used to pull in the Discover feed or emoji search.
// v1.0 - This requires hardcoding your username, discovery feed and/or emoji search.
//        Title field has been replaced with a specific URL so you can quickly
//        replay to a post on M.b web. Added a line so you can see the original posts 
//        author in your RSS feed author field.

function convert_jsonfeed_to_rss($content = NULL, $max = NULL)
    //Test if the content is actual JSON
    if( json_last_error() !== JSON_ERROR_NONE) return FALSE;

    //Now, is it valid JSONFeed?
    $jsonFeed = json_decode($content, TRUE);
    if (!isset($jsonFeed['version'])) return FALSE;
    if (!isset($jsonFeed['title'])) return FALSE;
    if (!isset($jsonFeed['items'])) return FALSE;

    //Decode the feed to a PHP array
    $jf = json_decode($content, TRUE);

    //Get the latest item publish date to use as the channel pubDate
    $latestDate = 0;
    foreach ($jf['items'] as $item) {
        if (strtotime($item['date_published']) > $latestDate) $latestDate = strtotime($item['date_published']);
    $lastBuildDate = date(DATE_RSS, $latestDate);

    //Create the RSS feed
    $xmlFeed = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"></rss>');

    //Required elements
    $xmlFeed->channel->addChild("title", $jf['title']);
    $xmlFeed->channel->addChild("pubDate", $lastBuildDate);
    $xmlFeed->channel->addChild("lastBuildDate", $lastBuildDate);

    //Optional elements
    if (isset($jf['description'])) $xmlFeed->channel->description = $jf['description'];
    if (isset($jf['home_page_url'])) $xmlFeed->channel->link = $jf['home_page_url'];

    foreach ($jf['items'] as $item) {
        $newItem = $xmlFeed->channel->addChild('item');

        //Standard stuff
//        if (isset($item['url'])) $newItem->addChild('guid', $item['url']);
// Replaced in order to add the line below allowing for a direct link to
// using the Post ID
        if (isset($item['id'])) $newItem->addChild('guid', "" . $item['id']);
        if (isset($item['title'])) $newItem->addChild('title', $item['title']);
        if (isset($item['content_text'])) $newItem->addChild('description', $item['content_text']);         
        if (isset($item['content_html'])) $newItem->addChild('description', $item['content_html']);
        if (isset($item['date_published'])) $newItem->addChild('pubDate', $item['date_published']);
        if (isset($item['url'])) $newItem->addChild('link', "" . $item['id']);
// Included the Author for who the post was created by in the specific entry
        if (isset($item['url'])) $newItem->addChild('author', $item['author']['name']);
        if(isset($item['attachments'])) {
            foreach($item['attachments'] as $attachment) {
                $enclosure = $newItem->addChild('enclosure');
                $enclosure['url'] = $attachment['url'];
                $enclosure['type'] = $attachment['mime_type'];
                $enclosure['length'] = $attachment['size_in_bytes'];

    //Make the output pretty
    $dom = new DOMDocument("1.0");
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    return $dom->saveXML();

//User Specific - Replace mb_username with your specific username
//Discover Feed - Replace mb_username.json with just discover or with emoji name like discover/running

$content = @file_get_contents("");

echo convert_jsonfeed_to_rss($content)."\n";