Opening and Ending Tag mismatch error while creating Rss

1.3k Views Asked by At

I am new in php nd want to help in generating rss feed. i have a db with name "test123" having fields Id, Name, Address, Designation and Text. Here is my code for rss feed`

<?php

function connect() {
    return new PDO('mysql:host=localhost;dbname=test123', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}

$pdo = connect();

// posts *******************************
$sql = 'SELECT * FROM form ORDER BY id DESC';
$query = $pdo->prepare($sql);
$query->execute();
$rs_post = $query->fetchAll();

// The XML structure
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>PACRA Rss Feed</title>';
$data .= '<link>http://www.pacra.com</link>';
$data .= '<description>Pacra Pakistan</description>';
foreach ($rs_post as $row) {
    $data .= '<item>';
    $data .= '<title>'.$row['Name'].'</title>';
    $data .= '<link>'.$row['Address'].'</link>';
    $data .= '<description>'.$row['Text'].'</description>';
    $data .= '</item>';
}
$data .= '</channel>';
$data .= '</rss> ';

header('Content-Type: application/xml');
echo $data;
?>`

Problem is that when i run that code it show an error message "Opening and Ending Tag mismatch" Here is the image of error Image Link

1

There are 1 best solutions below

3
On BEST ANSWER

It might be the header, I use the following in my PHP generated RSS feeds: header('Content-type: text/xml; charset=UTF-8');

And escape your <title> and <description> with CDATA.

Did you validate your feed using the RSS validator? https://validator.w3.org/feed/

Also my structure is a bit different:

     <?xml version="1.0" ?>
     <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
        <channel>
            <atom:link href="http://www.website.com" rel="self" type="application/rss+xml" />
            <title>RSS Title</title> 
            <link>http://www.website.com</link> 
            <description>Description</description>
            <language>en</language>
            <managingEditor>Name</managingEditor>
            <webMaster>[email protected]</webMaster>
            <item>
                <pubDate>Wed, 02 Oct 2015 15:00:00 +0200</pubDate>
                <title><![CDATA[Here the title]]></title>
                <link>http://www.website.com/page</link>
                <guid>http://www.website.com/page</guid>        
                <description><![CDATA[Here the content]]></description>
            </item>
            <item>
                <pubDate>Wed, 02 Oct 2015 15:00:00 +0200</pubDate>
                <title><![CDATA[Here the title2]]></title>
                <link>http://www.website.com/page2</link>
                <guid>http://www.website.com/page2</guid>       
                <description><![CDATA[Here the content2]]></description>
            </item>
        </channel>
    </rss>