[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP Soap XML parser: How To?

Posted on 2009-12-18
9
Medium Priority
?
863 Views
Last Modified: 2013-12-12
Im using PHP on linux and Im trying to read an incoming response that looks like the example below... I need to extract the XML between the <CheckResult> tags and then parse that xml into an array so I can pull out the attributes in the <domain tags... How do I do this? SimpleXML didnt work because the actual XML i need is inside the SOAP response. I assume I need to use soap to extract the XML then I can use Simple XML to get the data out but how do I go about doing this?

<?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><CheckResponse xmlns="http://domain.com/webservices/"><CheckResult><?xml version="1.0" encoding="UTF-16"?>
<check>
      <domain name="DOMAIN1.COM" avail="0" canorder="1"/>
      <domain name="DOMAIN2.COM" avail="1" canorder=""/>
      <domain name="DOMAIN3.COM" avail="1" canorder=""/>
</check>
</CheckResult></CheckResponse></soap:Body></soap:Envelope>
<?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><CheckResponse xmlns="http://domain.com/webservices/"><CheckResult><?xml version="1.0" encoding="UTF-16"?>
<check>
	<domain name="DOMAIN1.COM" avail="0" canorder="1"/>
	<domain name="DOMAIN2.COM" avail="1" canorder=""/>
	<domain name="DOMAIN3.COM" avail="1" canorder=""/>
</check>
</CheckResult></CheckResponse></soap:Body></soap:Envelope>

Open in new window

0
Comment
Question by:cimmer
  • 5
  • 4
9 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26082997
Where did this thing come from?  What you have here is grossly malformed - it has two conflicting XML header strings.  All I can think of to do is "mung" out the soap tags.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1776 total points
ID: 26083054
Run this and read over the code - see if it makes sense to you.  Best regards, ~Ray
<?php // RAY_unsoap.php
error_reporting(E_ALL);
echo "<pre>\n";
 
// THE TEST DATA STRING WITH SOAP JUNK IN IT
$str = <<<EOXML
<?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><CheckResponse xmlns="http://domain.com/webservices/"><CheckResult><?xml version="1.0" encoding="UTF-16"?>
<check>
        <domain name="DOMAIN1.COM" avail="0" canorder="1"/>
        <domain name="DOMAIN2.COM" avail="1" canorder=""/>
        <domain name="DOMAIN3.COM" avail="1" canorder=""/>
</check>
</CheckResult></CheckResponse></soap:Body></soap:Envelope>
EOXML;
 
// THE DELIMITERS
$alpha = '<CheckResult>';
$omega = '</CheckResult>';
 
// A COMPLETE LIE
$fubar = 'encoding="UTF-16"';
 
// ISOLATE THE MATERIAL BETWEEN THE DELIMITERS
$arr = explode($alpha, $str);
$arr = explode($omega, $arr[1]);
 
// REMOVE THE LIE TO LEAVE VALID XML
$xml = str_replace($fubar, '', $arr[0]);
 
// LOAD THE XML INTO AN OBJECT
$obj = SimpleXML_Load_String($xml);
 
// GET THE USEFUL STUFF OUT OF THIS OBJECT
var_dump($obj);

Open in new window

0
 

Author Comment

by:cimmer
ID: 26083104
This came from one of the largest domain registrars in the world. Its the response from their API. I cant change it so I gotta deal with what is there.
0
Technology Partners: 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: 26083123
Well, the snippet I posted above will deal with it.  As a matter of design patterns I have discovered that there is a LOT of malformed XML in the world, and so it is probably best to isolate the XML interpretation in your design.  Have a separate module that reads, mungs, cleans up and interprets the XML.  You can return an object, like the above, or you can return a clean string by calling the asXML() method.
0
 

Author Comment

by:cimmer
ID: 26083283
actually looking at the response again... the xml part actually comes across like this...

............
<CheckResult><?xml version="1.0" encoding="UTF-16"?>
<check>

      <domain name="DOMAIN1.COM" avail="0" canBackorder="1"/>
      <domain name="DOMAIN2.COM" avail="1" canBackorder=""/>
      <domain name="DOMAIN3.COM" avail="1" canBackorder=""/>
</check>
</CheckResult>
..........
0
 

Author Comment

by:cimmer
ID: 26083291
um... the above post didnt copy my code right... the < and > are suppose to be & l t ; and   & g l t ;
0
 

Author Comment

by:cimmer
ID: 26083334
i tweaked you code a lil bit but for the most part it worked perfect. I had to add the following to UN-encode the xml...

// REMOVE THE LIE TO LEAVE VALID XML
$xml = str_replace($fubar, '', $arr[0]);
// UN-encode the response
$xml = str_replace('<', '<', $xml );
$xml = str_replace('>', '>', $xml );

thanks ;)
0
 

Author Comment

by:cimmer
ID: 26083344
again this forum didnt show   & l t ;    and    & g l t ;   in my code above...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26083447
Interesting how EE mungs the ampersand-lt things, eh!  Anyhow, it looks like you are on the right track now.  Have a look at the htmlentity_decode function when you get a chance.  Best regards, ~Ray
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

834 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