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 https://readymadecode.com/feed/

<?php

$xml=simplexml_load_file("https://readymadecode.com/feed/") or die("Error: Cannot create object");

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

?>

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.

Recommended :

How to stop confirm form resubmission?

How to secure forms in PHP?

Read For more Tutorials about PHP

If you like this, share this.

Follow us on FacebookTwitterTumblrLinkedIn.