rss
twitter
  • Showcase
  • Carrières
  • Support
  • GitLab
  • Espace client
  • Contact

Kohana PHP framework : add images nodes to your RSS feeds

2

INDEX

Scope

This article is intended for use with Kohana 2.x version. I have tested only with the latest, which is 2.3.4 as of today.

Kohana’s « Feed » helper

The official documentation of the helper is located here :https://docs.kohanaphp.com/helpers/feed

With this helper you can easily create an RSS feed, however the method doesn’t natively support nested tags. So … won’t work and you’ll get this error :

SimpleXMLElement::addChild() expects parameter 2 to be string, array given

 Workaround : syntax in your controller

Logic tends to nest your tags into a sub-array, like this :

public function rss() {
        $info = array('title' => 'Christian Lapie', 'notes' => 'Latest updates');
        $items = ORM::factory('article')
                ->where('statut', 'publie')
                ->orderby('date', 'desc')
                ->find_all();
        $rssitems = array();
        foreach($items as $item) {
                $rssitems[] = array(
                        'title' => $item->titre,
                        'link' => Kohana::config('site.url_site').'/oeuvres/'.$item->id.'/'.url::title($item->titre),
                        'description' => $item->soustitre,
                        'author' => 'Christian Lapie',
                        'pubDate' => date("D, d M Y H:i:s +0100", strtotime($item->date)),
                        'image' => array(
                                'url' => Kohana::config('site.url_site').'/IMG/arton'.$item->id.'.jpg',
                                'title' => $item->titre,
                                'link' => Kohana::config('site.url_site').'/oeuvres/'.$item->id.'/'.url::title($item->titre)
                                )
                        );
        }
        echo feed::create($info, $rssitems);
        exit();
}

(look carefully at the image key and its sub-pairs)

 Editing Kohana’s core

It turns out to be pretty simple.
You will open the following file :

/system/helpers/feed.php

Then find the following lines (around 114)  :

// Add the info to the row
$row->addChild($name, $value);

Finally replace the block with this :

// Add the info to the row
if (is_array($value)) {
        $temp = $row->addChild($name);
        foreach($value as $key1 => $value1) {
                $temp->addChild($key1, $value1);
        }
} else {
        $row->addChild($name, $value);
}

Et voilà !
Now you can have a nicely valid  tag :

 Result

<item>
        <title>Les secrets d'un temps immense</title>
        <link>https://lapie.localhost/oeuvres/318/les-secrets-dun-temps-immense</link>
        <description>Varsovie, Blonie, Pologne (Poland)</description>
        <author>Christian Lapie</author>
        <pubDate>Wed, 12 Jan 2011 00:00:00 +0100</pubDate>
        <image>
                <url>https://lapie.localhost/IMG/arton318.jpg</url>
                <title>Les secrets d'un temps immense</title>
                <link>https://lapie.localhost/oeuvres/318/les-secrets-dun-temps-immense</link>
                </image>
</item>

You can add optional parameters such as Width, Height… see the RSS Specifications here.

Please leave comment if you have found another (official) solution !Retour ligne automatique
Germain

DATE 12 Avr 2011
by : Germain
Author / Auteur

Social Share / Partager

    2 Comments
    1. hyip 11 mars 2015 at 6 h 05 min Répondre

      Great post. Exactly solved my problem.
      In my case the file that need to be revised is located here:
      SYSPATH/classes/Kohana/Feed.php [ 163 ]
      Thanks for sharing.

      • Germain 11 mars 2015 at 9 h 43 min Répondre

        Glad it helped!

    Répondre à Germain Annuler la réponse

    *
    *

    More in Back-end : PHP, CMS

    • Quel php.ini (MAMP ou Linux), quelle valeur de phpinfo() ?
    • Trucs utiles pour WordPress
    • Les bons headers PHP pour envoyer du CSV compatible Excel en UTF8
    • Imagemanager(tiny Mce) et kohana
    • Récupérer, vérifier et exploiter une date

    Search the blog

    Blog categories

    • Workflow (1)
    • Front-end : HTML, CSS (13)
    • Front-end : javascript, jQuery (33)
    • Back-end : PHP, CMS (42)
    • Back-end : SQL, MySQL (20)
    • Sysadmin : Linux, Apache, GIT (75)
    • Designers : tools, advice (2)
    • Desktop : OS X (37)
    • Desktop : Microsoft (12)
    • Uncategorized (8)

    ITALIC™ Resources

    • Paramètres de connexion au serveur mail
    • Graphistes : séduire un développeur web
    • Dév front : nos bonnes pratiques
    • Mailing : nos (bonnes ?) pratiques
    • Environnement de travail Mac

    Recent posts

    • Autoriser le chargement de contenus mixtes non-https dans Chrome (Mac)
    • Afficher les logs MySQL
    • Recevoir un mail lors de la connexion root à un serveur
    • Changer l’interclassement de plusieurs tables MySQL à la fois (ou presque
    • Maildev : tester l’envoi de mails durant ses développements
    © 2008-2017 ITALIC™ • 8 bis rue Abel • 75012 PARIS • Tel +33 (0)1 48 44 46 35 • RCS PARIS 508 228 772 • Powered by WordPress & GoodLayers
    Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site web.
    Cookie settingsACCEPTER
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Toujours activé
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    Non-necessary
    Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
    Enregistrer & appliquer