We help IT Professionals succeed at work.

PHP CURL SOAP XML to Array

Hi,

I am getting the following SOAP output from a returned CURL message. I am looking to get the below returned variables and put them into an array or some form of usable data. Where I get the name etc... in a usable format.

Using PHP what is the best way? I have looked to use json but can't get it to work.

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:getUserResponse xmlns:ns="http://www.cisco.com/AXL/API/10.5"><return><user uuid="{3E0D6C9A-60AD-CFEB-9FF9-D74AB9B5A5C2}"><firstName>Mark</firstName><middleName/><lastName>Test</lastName><userid>mtest</userid><mailid/><department>IT</department><manager/><userLocale /></user></return></ns:getUserResponse></soapenv:Body></soapenv:Envelope>

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
XML and JSON are both transport mechanisms, but they use different data formats.  Usually the best way to get XML data strings into a usable form is to use SimpleXML.  It's documented in the online PHP manual.   I'll try to give you an example.
Most Valuable Expert 2011
Top Expert 2016
Commented:
This will decode the XML.  If it feels like a bit of a hack, that's because PHP and SOAP do not really play very well together, but at least it shows the notation to get to the data elements inside the XML data.  If you can get this data in JSON format, it will probably be easier to use -- nobody uses SOAP any more except on legacy projects where there is too much cost to convert.  Or on Government sites, where they don't understand the advantages of JSON!

<?php // demo/temp_mark_06.php

/**
 * Decode XML
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28637690.html
 */
error_reporting(E_ALL);
echo '<pre>';

$xml = <<<EOD
<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:getUserResponse xmlns:ns="http://www.cisco.com/AXL/API/10.5"><return><user uuid="{3E0D6C9A-60AD-CFEB-9FF9-D74AB9B5A5C2}"><firstName>Mark</firstName><middleName/><lastName>Test</lastName><userid>mtest</userid><mailid/><department>IT</department><manager/><userLocale /></user></return></ns:getUserResponse></soapenv:Body></soapenv:Envelope>
EOD;

$txt = mungXML($xml);
$obj = SimpleXML_Load_String($txt, 'SimpleXMLElement', LIBXML_NOCDATA);

// SHOW HOW TO TEASE OUT A DATA ELEMENT
$nom = (string)$obj->soapenv_Body->ns_getUserResponse->return->user->firstName;
var_dump($nom);

// SHOW THE ENTIRE DATA ELEMENT
var_dump($obj);


// FUNCTION TO MUNG THE XML SO WE DO NOT HAVE TO DEAL WITH NAMESPACE
function mungXML($xml)
{
    $obj = SimpleXML_Load_String($xml);
    if ($obj === FALSE) return $xml;

    // GET NAMESPACES, IF ANY
    $nss = $obj->getNamespaces(TRUE);
    if (empty($nss)) return $xml;

    // CHANGE ns: INTO ns_
    $nsm = array_keys($nss);
    foreach ($nsm as $key)
    {
        // A REGULAR EXPRESSION TO MUNG THE XML
        $rgx
        = '#'               // REGEX DELIMITER
        . '('               // GROUP PATTERN 1
        . '\<'              // LOCATE A LEFT WICKET
        . '/?'              // MAYBE FOLLOWED BY A SLASH
        . preg_quote($key)  // THE NAMESPACE
        . ')'               // END GROUP PATTERN
        . '('               // GROUP PATTERN 2
        . ':{1}'            // A COLON (EXACTLY ONE)
        . ')'               // END GROUP PATTERN
        . '#'               // REGEX DELIMITER
        ;
        // INSERT THE UNDERSCORE INTO THE TAG NAME
        $rep
        = '$1'          // BACKREFERENCE TO GROUP 1
        . '_'           // LITERAL UNDERSCORE IN PLACE OF GROUP 2
        ;
        // PERFORM THE REPLACEMENT
        $xml =  preg_replace($rgx, $rep, $xml);
    }
    return $xml;
}

Open in new window