PHP Ajax RSS Feed Example

please click here for more wordpress cource

Sure! Here is an example of using PHP and Ajax to display an RSS feed:


<!DOCTYPE html>
    <meta charset="UTF-8">
    <title>Ajax RSS Feed Example</title>
    <script src=""></script>
        $(document).ready(function() {
                url: 'rss.php',
                type: 'GET',
                dataType: 'xml',
                success: function(xml) {
                    $(xml).find('item').each(function() {
                        var title = $(this).find('title').text();
                        var link = $(this).find('link').text();
                        var description = $(this).find('description').text();
                        $('#feed').append('<div class="item"><a href="' + link + '">' + title + '</a><br />' + description + '</div>');
    <h1>RSS Feed</h1>
    <div id="feed"></div>


    $rss = new DOMDocument();
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        array_push($feed, $item);

    header('Content-Type: text/xml; charset=utf-8');
    echo '<?xml version="1.0" encoding="UTF-8"?>';
<rss version="2.0">
        <title>Example RSS Feed</title>
        <description>Example RSS Feed</description>
        <?php foreach ($feed as $item): ?>
            <title><?php echo $item['title']; ?></title>
            <link><?php echo $item['link']; ?></link>
            <description><?php echo $item['description']; ?></description>
        <?php endforeach; ?>

In this example, the index.php file contains the HTML and JavaScript code for displaying the RSS feed, while the rss.php file retrieves the RSS feed from a URL and converts it to an XML format that can be processed by JavaScript. The jQuery library is used to make an Ajax request to the rss.php file and parse the XML response to display the feed on the webpage.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *