This tutorial should summarize the possibilities of XML handling by PHP. After short introduction what XML can be used for, we’ll move to the PHP world and show how this web oriented programming language supports XML data format.
I personaly don’t know anybody who doesn’t know RSS. But to be sure: RSS is an abbreviation for Real Simple Syndication and it’s one of the most widespread content publication technologies. Why am I talking about RSS if post title indicates something about PHP and XML? Well, as most of you probably know, RSS uses the XML standard to carry data. For those, who would like to refresh what XML is about, please navigate yourselves here. And those who don’t know what PHP is, please navigate here. Well, now we can move forward to the main topic of this tutorial post.
PHP offers several forms of XML maninupation. You can choose from them in accordance with your preferences and extension accessibilities. I’m writing this because not all extensions must be present or enabled on the system you are using. In the case of required extension (or its part – e.g. some function) absence, the PHP will probably let you know about it by returning error message. If you are admin of the system where PHP runs, there is no problem to adjust the PHP directives, load necessary extension (if compiled already) and continue in work. But if the PHP is not under your control, you have to ask an administrator for an assistance.
Well, let’s suppose that PHP is running and all necessary extensions are successfully loaded, thus available. From this point the situation is more than simple. Why? Because PHP has really extensive support of XML manipulation. There’s just up to you, what’s more friendly to you to use.
PHP XML Manipulation Extensions
As you can see from the stated above, there is no problem manipulate XML by PHP. OK, but this was just useless theory so far… How can we use these extensions to get requested data from an XML file? Well, the answer is right here. Take a look at the PHP source code below I’ve pasted from my PHP IDE. By this example I tried to explain you how to use possibilities of PHP XML extensions, in this case the SimpleXML one. By copying and pasting this source code into a .php file you’ll immediately have very basic RSS reader of this blog. Don’t believe? Try and enjoy!
<?php /** * This is a demonstration script related to the following post: * @link http://www.webdevelopmentstuff.com/105/php-and-xml.html * @author Teddy Cyber <teddy@webdevstuff.com> * @license GNU GPL * * This script demonstrates PHP manipulation by SimpleXML extension. * For better understanding, simplicity and cleanliness, this code * is NOT treated for errors. Use it on your own risk. */ /** * Leave PHP display errors, if any. * Display errors for debug only, turn it off in normal operation. */ ini_set("display_errors", "on"); /** * Enter the feed URL. */ $feed = "https://www.webdevstuff.com/wp-feed.php"; /** * Load whole XML file into SimpleXMLElement Object. */ $xml = simplexml_load_file($feed); /** * Uncomment following line to see the raw output. */ //echo '<pre>'; print_r($xml); echo '</pre>'; /** * Format presentable output from SimpleXMLElement Object. * For better understanding uncomment the line of code above * to see the raw output, if it's not obvious what comes where from. */ $s1 = 'style="font-size:1.7em"'; $s2 = 'style="text-transform:uppercase;font-size:1.2em"'; $header = '<strong ' . $s1 . '>' . $xml->channel->title . '</strong><br />'; $header .= '<span ' . $s2 . '>' . $xml->channel->description . '</span><br />'; $header .= '<br />'; echo $header; foreach ($xml->channel->item as $item) { $output = '<a href="' . $item->link . '">' . $item->title . '</a><br />'; $output .= 'Published on ' . $item->pubDate . '<br />'; $output .= $item->description . '<br />'; $output .= '<br />'; echo $output; } ?>
I hope you’ve been successfull in practice of stated above simple feed reader and the comments of source code were explanatory enough to understand the basics of XML manipulation by PHP. If not, you are welcome to post your questions in comments and I’m sure that either me or some other post reader will try answer you.