Solved

Trouble parsing soap xml result

Posted on 2016-10-28
3
49 Views
Last Modified: 2016-10-28
Hi,
I can't seem to figure out the best way to parse the result of my curl/soap request.
For example, if all I was trying to do was get the BaseMSR value for below, how do I echo it out? Can't seem to access the value on its own. In a perfect world I'd convert it to json for the return

Code in my controller is:

$request = '
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>

<VehicleGet xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PBS.PartnerHub.ServiceModel">
  <IncludeBuildVehicles>false</IncludeBuildVehicles>
  <IncludeInactive>false</IncludeInactive>
  <SerialNumber>9991.QA</SerialNumber>
  <VIN>WVGEF9BP1ED005013</VIN>
  <Year>2014</Year>
  <Year>VOLKSWAGEN</Year>
</VehicleGet>

</soap:Body>
</soap:Envelope>
';


$soapURL ="https://partnerhub.mydomain.com";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $soapURL);
	$header[] = "Content-type: text/xml; charset=utf-8";
	$header[] = "SOAPAction: VehicleGet";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	
	$result = curl_exec($ch);
	
	curl_close($ch);
	
	dd($result);

Open in new window


Response below:
"<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<VehicleGetResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PBS.PartnerHub.ServiceModel">
<Vehicles>
<Vehicle>
<Air>100.000000</Air>
<AppraisedValue>0.000000</AppraisedValue>
<BaseMSR>50975.000000</BaseMSR>
<Year>2014</Year>
</Vehicle>
</Vehicles>
</VehicleGetResponse>
</s:Body>
</s:Envelope>"

Open in new window

0
Comment
Question by:tjyoung
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41864147
This is a bit of a hack, but it works.  SOAP is just awful, isn't it!
https://iconoun.com/demo/temp_tjyoung.php
<?php // demo/temp_tjyoung.php
/**
 * https://www.experts-exchange.com/questions/28979608/Trouble-parsing-soap-xml-result.html
 */
error_reporting(E_ALL);

$xml = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<VehicleGetResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PBS.PartnerHub.ServiceModel">
<Vehicles>
<Vehicle>
<Air>100.000000</Air>
<AppraisedValue>0.000000</AppraisedValue>
<BaseMSR>50975.000000</BaseMSR>
<Year>2014</Year>
</Vehicle>
</Vehicles>
</VehicleGetResponse>
</s:Body>
</s:Envelope>
EOD;

// MUNG THE XML AND MAKE AN OBJECT
$xml = mungXML($xml);
$obj = SimpleXML_Load_String($xml);

// EXTRACT THE DATA
$vehicle = $obj->s_Body->VehicleGetResponse->Vehicles->Vehicle;
$basemsr = $vehicle->BaseMSR;
var_dump($basemsr);

// 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

Outputs:
object(SimpleXMLElement)#4 (1) { [0]=> string(12) "50975.000000" }

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41864176
Worked right out of the box and using a function is much cleaner in my case.
Thanks again. Always so concise.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41864218
Glad to help!  Thanks for the points and thanks for using E-E, ~Ray
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question