Fetching XML from a remote site

PHP has provided with a wonderful function file_get_contents

We first start with getting the xml feed into a string, by using file_get_contents

//the $url is the link from where XML is received
$url = 'http://devshed.com/index2.php?option=mos_rss&no_html=1'; 
$con = file_get_contents($url); //the contents are in the variable $con

//now create the SAX xml parser $xp = xml_parser_create();

Now using the function xml_parse_into_struct, we parse the xml and create 2 arrays $vals and $index, The $index contains all the info about the node nos. While the most important array is the $vals array

xml_parse_into_struct($xp, $con, $vals, $index);
// we now free the xml parser. xml_parser_free($xp);

If we print_r the $vals array we see something like this

Array
(
	[0] => Array
		(
			[tag] => RSS
			[type] => open
			[level] => 1
			[attributes] => Array
			    (
                    [VERSION] => 2.0
                )

            [value] => 

        )

    [1] => Array
        (
            [tag] => CHANNEL
            [type] => open
            [level] => 2
            [value] => 
	
        )

    [2] => Array
        (
            [tag] => TITLE
            [type] => complete
            [level] => 3
            [value] => Dev Shed - The Open...
        )

		.....
		Something like this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.