May 15, 2023

How To Display Another Website RSS Content On Yours Using PHP, HTML & CSS

How To Display Another Website RSS Content On Yours Using PHP, HTML & CSS

What is an RSS Feed?

An RSS feed, which stands for Really Simple Syndication, is a standardized web format used to publish frequently updated content. It allows users to subscribe to websites and receive automatic updates whenever new content, such as news articles or blog posts, is published.

RSS feeds provide a structured way to distribute and access information from multiple sources without having to visit each website individually.

To display a website feed on another website homepage using PHP, you can use the SimpleXMLElement class to parse the RSS or Atom feed and display the relevant content.

Here is an example PHP code that demonstrates how to display the latest articles from a feed:

<?php
// URL of the feed
$url = 'https://example.com/feed';

// Fetch the feed content
$xml = file_get_contents($url);

// Parse the XML content
$feed = new SimpleXMLElement($xml);

// Display the latest articles
foreach ($feed->channel->item as $item) {
    echo '<h3><a href="' . $item->link . '">' . $item->title . '</a></h3>';
    echo '<p>' . $item->description . '</p>';
    echo '<p><em>Published on ' . date('F j, Y', strtotime($item->pubDate)) . '</em></p>';
}
?>

In this code, we first specify the URL of the feed we want to display using the $url variable. We then use the file_get_contents function to fetch the feed content as an XML string.

Next, we create a new instance of the SimpleXMLElement class and pass the feed content to it to parse the XML data.

Finally, we use a foreach loop to iterate over the item elements in the feed and display the title, description, and publication date of each article using the echo statement. You can customize the output HTML to match the style and design of your website.

If you’re benched on how to generate the images from the post, here is an updated code below to help you:

<?php
// URL of the feed
$url = 'https://example.com/feed';

// Fetch the feed content
$xml = file_get_contents($url);

// Parse the XML content
$feed = new SimpleXMLElement($xml);

// Display the latest articles
foreach ($feed->channel->item as $item) {
    echo '<h3><a href="' . $item->link . '">' . $item->title . '</a></h3>';
    echo '<p>' . $item->description . '</p>';
    echo '<p><em>Published on ' . date('F j, Y', strtotime($item->pubDate)) . '</em></p>';

    // Check if the item has an image
    if ($item->enclosure['type'] == 'image/jpeg' || $item->enclosure['type'] == 'image/png') {
        echo '<img src="' . $item->enclosure['url'] . '">';
    }
}
?>

In this updated code, we first check if the item has an image by looking for the enclosure element in the XML data. If the item has an image, we display it using an <img> tag with the URL of the image from the enclosure element.

Note that not all feed items will have an image associated with them, so it’s important to include this check to avoid any errors.

Additionally, some feeds may use different formats or tags for images, so you may need to modify this code to match the specific structure of the feed you are working with.

To style the output of the feed on your website, you can add CSS to your HTML code. Here’s an example of how you can modify the previous code to style the output:

<?php
// URL of the feed
$url = 'https://example.com/feed';

// Fetch the feed content
$xml = file_get_contents($url);

// Parse the XML content
$feed = new SimpleXMLElement($xml);

// Display the latest articles
foreach ($feed->channel->item as $item) {
    echo '<div class="feed-item">';
    echo '<h3><a href="' . $item->link . '">' . $item->title . '</a></h3>';
    echo '<p class="feed-item-description">' . $item->description . '</p>';
    echo '<p class="feed-item-date"><em>Published on ' . date('F j, Y', strtotime($item->pubDate)) . '</em></p>';

    // Check if the item has an image
    if ($item->enclosure['type'] == 'image/jpeg' || $item->enclosure['type'] == 'image/png') {
        echo '<img class="feed-item-image" src="' . $item->enclosure['url'] . '">';
    }
    echo '</div>';
}
?>

<!-- CSS styles -->
<style>
    .feed-item {
        margin-bottom: 20px;
        border: 1px solid #ccc;
        padding: 10px;
    }

    .feed-item h3 {
        margin-top: 0;
    }

    .feed-item-description {
        font-size: 16px;
        line-height: 1.5;
    }

    .feed-item-date {
        font-style: italic;
    }

    .feed-item-image {
        max-width: 100%;
        height: auto;
        display: block;
        margin: 10px auto;
    }
</style>

In this updated code, we’ve added a div element with a class of feed-item around each item to provide a container for the content. We’ve also added some CSS styles to adjust the layout and typography of the feed items. Finally, we’ve added a CSS style for the image to ensure it is properly sized and centered.

You can modify the CSS styles to match the design of your website. For example, you could change the border color, font size, or text alignment to fit with the overall look and feel of your site.