DEV Community

Cover image for How to read Feeds in PHP
Chetan Rohilla
Chetan Rohilla

Posted on • Updated on • Originally published at w3courses.org

How to read Feeds in PHP

A Feeds is an xml file which contains the information about website. For an example xml file of E-Commerce website contains information about products, categories, offers, tags. The data in xml(Extensible Markup Language) file contains in the form of structured tags. In this article, we will learn how to read feeds in php.

Here we will read the xml feeds of our website URL w3courses.org/feed/

<?php

$xml=simplexml_load_file("https://www.w3courses.org/feed/") or die("Error: Cannot create object");

foreach ($xml->channel->item as $key => $value) {
    print_r($value);
}

?>
Enter fullscreen mode Exit fullscreen mode

We used the function simplexml_load_file to read the feeds in PHP. SimpleXML is a PHP extension that allows us to easily manipulate and get XML data. Inside the print_r we have the information about item tags.

The SimpleXML Parser in PHP
SimpleXML is a tree-based parser. It provides an easy way of getting an element’s name, attributes and textual content if you know the XML document’s structure or layout.

SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays and objects.

Compared to DOM or the Expat parser, SimpleXML takes a fewer lines of code to read text data from an element.


Please like share and give positive feedback to motivate me to write more.

For more tutorials visit my website or follow me.

Thanks:)
Happy Coding:)

Top comments (0)