[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Trouble parsing soap xml result

Posted on 2016-10-28
3
Medium Priority
?
128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

649 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