Write a php script to scrap blog content from feed

Learn how to write a PHP script to scrape and extract blog content from an RSS feed, useful for aggregating or displaying content on a website.

1 Answer

1

Certainly, here's a simple PHP script that uses the SimpleXML extension to scrape blog content from an RSS or Atom feed:


```php

<?php

// Specify the URL of the RSS or Atom feed you want to scrape

$feedUrl = 'https://example.com/blog/feed';


// Fetch the feed content

$feedContent = file_get_contents($feedUrl);


if ($feedContent) {

    // Parse the feed using SimpleXML

    $feed = simplexml_load_string($feedContent);


    if ($feed) {

        // Loop through the feed items

        foreach ($feed->channel->item as $item) {

            // Extract relevant information, e.g., title, link, description

            $title = (string)$item->title;

            $link = (string)$item->link;

            $description = (string)$item->description;


            // You can output or store this information as needed

            echo "Title: $title<br>";

            echo "Link: $link<br>";

            echo "Description: $description<br>";

            echo "<hr>";

        }

    } else {

        echo "Failed to parse the feed.";

    }

} else {

    echo "Failed to fetch the feed content.";

}

?>

```


Replace `$feedUrl` with the URL of the RSS or Atom feed you want to scrape. This script fetches the feed, parses it using SimpleXML, and extracts information from each feed item, such as title, link, and description. You can modify it to suit your specific needs.

avatar
Rakan
18 Ques 1 Ans
answered 29 Oct 2023

Your Answer

undraw-questions

Login or Create Account to answer this question.

Do you have any opinion about Write a php script to scrap blog content from feed?

Login / Signup

Answers Adda Q&A communities are different.
Here's how

bubble
Knowledge sharing.

Question and answer communities are a great way to share knowledge. People can ask questions about any topic they're curious about, and other members of the community can provide answers based on their knowledge and expertise.

vote
Engagement and connection

These communities offer a way to engage with like-minded individuals who share similar interests. Members can connect with each other through shared experiences, knowledge, and advice, building relationships that extend beyond just answering questions..

check
Community building.

Answers Adda Question & Answer communities provide a platform for individuals to connect with like-minded people who share similar interests. This can help to build a sense of community and foster relationships among members.