?
Solved

Handling return vars in a SOAP response

Posted on 2009-02-22
6
Medium Priority
?
1,533 Views
Last Modified: 2013-11-18
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
Comment
Question by:hallikpapa
  • 4
  • 2
6 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23706555
You can parse this easily with SimpleXML_Load_String()

I'll give you an example, ~Ray
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 23706645

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

Author Comment

by:hallikpapa
ID: 23706648
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

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

;-)

Ray
0
 

Author Comment

by:hallikpapa
ID: 23706657
Your example worked great, THANKS!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23706660
Thanks for the points and your kind words.  This is a great question! ~Ray
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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