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

LVL 6
mark_06Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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.
0
Ray PaseurCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.