Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create <content:encoded> in RSS

Tags:

php

rss

My RSS create by php like this:

<?php
include('dataservice.php');
include('data/article.php');
include('bussiness/article.php');
function sumary($string,$len)
{
    if($len > strlen($string)){$len=strlen($string);};
    $pos = strpos($string, ' ', $len);
    if($pos){$string = substr($string,0,$pos);}else{$string = substr($string,0,$len);}    
    return $string." ...";
}
$article = new articlebs();
    $data_article = $article->Getdata(); 
    $Sum = $data_article == false ? 0 : count($data_article);
    if($Sum!= 0)
    {
        header('Content-Type: text/xml; charset=utf-8'."\n");

        echo ('<rss xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">'."\n");
        echo ('<channel>'."\n"); 
        echo('<title>Test</title>'."\n");
        echo('<link>http://localhost</link>'."\n");
        echo('<description>Test RSS</description>'."\n");

        for($i = 0; $i < $Sum; $i++)
        {
            $Id_Article = $data_article[$i]['Id_Article'];
            $Title   = $data_article[$i]['Title'];
            $Content   = $data_article[$i]['Content'];
            $Time   = $data_article[$i]['Time'];
            $Id_User   = $data_article[$i]['Id_User'];

            preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $Content, $image);

            $image = preg_replace( '/(width|height)="\d*"\s/', "", $image );
            $image = preg_replace( '/"/', "", $image );
            $image = preg_replace( '/height=\d*/', "", $image );
            $image = preg_replace( '/(width|height)=\d*/', "", $image );

            echo('<item>'."\n");

            echo ('<title>').$Title.('</title>'."\n");
            if( empty( $image ) )
            {
                echo  ('<image>http://localhost/images/noimg.jpg</image>'."\n");
            }
            else
            {
                echo ('<image>'). $image['src'].('</image>'."\n");
            }  
            echo ('<link><![CDATA[')."http://localhost/index.php?detail&id=".$Id_Article.(']]></link>'."\n");
            echo ('<pubDate>').$Time.('</pubDate>'."\n"); 
            echo ('<guid><![CDATA[').$Content.(']]></guid>'."\n"); 
            echo ('<description><![CDATA[');
            echo strip_tags(sumary($Content,500));
            echo (']]></description>'."\n");
            echo ('<content:encoded> <![CDATA['.$Content.']]> </content:encoded>'); // i think error here
            echo ('</item>'."\n");

        }
        echo ('</channel>'."\n");
        echo ('</rss>');
    }
  ?>

When i run this RSS, it give an error:

Namespace prefix content on encoded is not defined.

I try to Google it but i have not find the way to fix it. Please help me fix it. Thank you so much

like image 434
Social Code Avatar asked Oct 19 '15 10:10

Social Code


1 Answers

You need to add the namespace declaration xmlns:content="http://purl.org/rss/1.0/modules/content/" to your <rss> element.

like image 189
user3942918 Avatar answered Nov 19 '22 18:11

user3942918