Solved

Trouble parsing soap xml result

Posted on 2016-10-28
3
27 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 108

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 108

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now