• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1545
  • Last Modified:

Handling return vars in a SOAP response

I am using the PHP5 SOAP library to call a web service, and receiving the response just fine. Is there something built into PHP that allows me to just grab the values returned back easily, or do I need to do some kind of XML Parsing? Here is an example of my XML response that is returned after I make the call. I just want to get the

<FYStartDate>$var</FYStartDate>
<FYEndDate>$var2</FYEndDate>

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soap:Body>
		<ReturnFYDatesResponse xmlns="http://mydomain.com/">
			<ReturnFYDatesResult>
				<xs:schema id="NewDataSet" xmlns="" 
					xmlns:xs="http://www.w3.org/2001/XMLSchema" 
					xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
					<xs:element name="NewDataSet" 
						msdata:IsDataSet="true" 
						msdata:UseCurrentLocale="true">
						<xs:complexType>
							<xs:choice minOccurs="0" maxOccurs="unbounded">
								<xs:element name="fiscalyeardates">
									<xs:complexType>
										<xs:sequence>
											<xs:element name="FYStartDate" type="xs:string" minOccurs="0" />
											<xs:element name="FYEndDate" type="xs:string" minOccurs="0" />
										</xs:sequence>
									</xs:complexType>
								</xs:element>
							</xs:choice>
						</xs:complexType>
					</xs:element>
				</xs:schema>
				<diffgr:diffgram 
					xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" 
					xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
					<NewDataSet xmlns="">
						<fiscalyeardates diffgr:id="fiscalyeardates1" msdata:rowOrder="0">
							<FYStartDate>10/01/2007</FYStartDate>
							<FYEndDate>09/30/2008</FYEndDate>
						</fiscalyeardates>
					</NewDataSet>
				</diffgr:diffgram>
			</ReturnFYDatesResult>
		</ReturnFYDatesResponse>
	</soap:Body>
</soap:Envelope>

Open in new window

0
hallikpapa
Asked:
hallikpapa
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
You can parse this easily with SimpleXML_Load_String()

I'll give you an example, ~Ray
0
 
Ray PaseurCommented:

<?php // RAY_parse_xml3.php
echo "<pre>";
 
// TEST DATA
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
                <ReturnFYDatesResponse xmlns="http://mydomain.com/">
                        <ReturnFYDatesResult>
                                <xs:schema id="NewDataSet" xmlns=""
                                        xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                                        <xs:element name="NewDataSet"
                                                msdata:IsDataSet="true"
                                                msdata:UseCurrentLocale="true">
                                                <xs:complexType>
                                                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                                                <xs:element name="fiscalyeardates">
                                                                        <xs:complexType>
                                                                                <xs:sequence>
                                                                                        <xs:element name="FYStartDate" type="xs:string" minOccurs="0" />
                                                                                        <xs:element name="FYEndDate" type="xs:string" minOccurs="0" />
                                                                                </xs:sequence>
                                                                        </xs:complexType>
                                                                </xs:element>
                                                        </xs:choice>
                                                </xs:complexType>
                                        </xs:element>
                                </xs:schema>
                                <diffgr:diffgram
                                        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
                                        xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
                                        <NewDataSet xmlns="">
                                                <fiscalyeardates diffgr:id="fiscalyeardates1" msdata:rowOrder="0">
                                                        <FYStartDate>10/01/2007</FYStartDate>
                                                        <FYEndDate>09/30/2008</FYEndDate>
                                                </fiscalyeardates>
                                        </NewDataSet>
                                </diffgr:diffgram>
                        </ReturnFYDatesResult>
                </ReturnFYDatesResponse>
        </soap:Body>
</soap:Envelope>';
 
// MUNG OUT THE COLONS
$xml = str_replace(':', '_', $xml);
 
// SUPPRESS WARNING MESSAGES FOR LOADING THE STRING
error_reporting(E_NONE);
 
// MAKE AN OBJECT
if (!$obj = SimpleXML_Load_String($xml)) die("FATAL ERROR LOADING XML");
 
// RESTORE ERROR REPORTING
error_reporting(E_ALL);
 
// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);
 
// PARSE TO THE THING WE WANT
$thing = $obj->soap_Body->ReturnFYDatesResponse->ReturnFYDatesResult->diffgr_diffgram->NewDataSet->fiscalyeardates;
 
// SHOW THE DATA FIELDS
$start = $thing->FYStartDate;
$end   = $thing->FYEndDate;
echo "<br/>Start = $start and End = $end \n";

Open in new window

0
 
hallikpapaAuthor Commented:
Cool, thanks.

$response contains the XML SOAP response I posted above, but $xml contains an empty object when I do a var_dump:


object(SimpleXMLElement)#2 (0) { }


$response = $client->__getLastResponse();
$xml = simplexml_load_string($response);
var_dump($xml);

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Ray PaseurCommented:
Yep, that's why I mung out the colons!  Yet another argument for a RESTful interface instead of SOAP!

;-)

Ray
0
 
hallikpapaAuthor Commented:
Your example worked great, THANKS!
0
 
Ray PaseurCommented:
Thanks for the points and your kind words.  This is a great question! ~Ray
0
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now