Read xml data in php

September 14, 2011 | In: PHP

PHP: Hypertext Preprocessor programming language, a popular dynamic web page scripting language, features the built-in SimpleXML extension, which simplifies reading and using XML in your PHP scripts. Read an RSS feed, XML office documents, or your own custom XML document.

Here we use SimpleXML library, which is not only the best for converting string to XML object but is also built into PHP core so there is no need to install it. Once you have the URL of the XML feed that you are going to use, you need to have PHP load the contents of the feed into a string variable. Using file_get_contents, you could fetch the XML file like so:


How to Read an XML File With PHP

<?php	
$xmlStr = file_get_contents('http://www.youdomain.com/feeds/news.xml');
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
echo "<pre>";print_r($arrXml);echo "</pre>";
?>

Here is the function to convert XMLObject into Array

<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
   
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }
   
    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}
?>

Other way to read XML data with cURL

cURL to read RSS feed XML+PHP

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.youdomain.com/feeds/news.xml');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
?>

Comments

comments

Powered by Facebook Comments

Related Posts



5 Responses to iDeal Payment Integration

Avatar

greece escorts

January 27th, 2013 at 2:53 am

It is actually a great and useful piece of information. I am glad that you just shared this helpful info with us. Please stay us informed like this. Thanks for sharing.

Avatar

xtrade.info

June 10th, 2013 at 2:19 pm

_Five Reasons Why You Should Go With PHP | PHP Development | Web Development India_ ended up being a great blog.
If only there was way more personal blogs like this amazing one on
the actual cyberspace. Anyway, many thanks for ur time, Chang

Avatar

accounting firms melbourne

July 23rd, 2013 at 5:32 pm

Hello there, just became aware of your blog through Google, and
found that it’s really informative. I am going to watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

Avatar

Cardio Before or After Lifting

August 22nd, 2013 at 4:33 am

You can certainly see your enthusiasm in the article you write.

The world hopes for more passionate writers like you who aren’t afraid to say how they believe. At all times go after your heart.

Avatar

create a sign online

January 4th, 2014 at 7:33 pm

hello!,I really like your writing so so much! percentage we keep up a correspondence extra about your post on AOL?
I require an expert in this house to solve my problem.
May be that’s you! Taking a look forward to peer you.

Leave a Reply

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

*


*

 

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>