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

$xmlStr = file_get_contents('');
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
echo "<pre>";print_r($arrXml);echo "</pre>";

Here is the function to convert XMLObject into Array

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)) {
            $arrData[$index] = $value;
    return $arrData;

Other way to read XML data with cURL

cURL to read RSS feed XML+PHP

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);

Related Posts

2 Responses to iDeal Payment Integration



June 25th, 2015 at 2:31 am

Somebody essentially help to make critically articles
I would state. This is the first time I frequented your website page and up to now?
I surprised with the analysis you made how to feng shui your bedroom (Nestor) make this
particular submit extraordinary. Magnificent process!


curtains and window coverings

June 25th, 2015 at 10:04 am

Thanks for the good writeup. It in reality was a enjoyment account it.
Glance complicated to far added agreeable from you! By the
way, how can we be in contact?

Also visit my website: curtains and window coverings

Leave a Reply

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