Solved

Can't parse XML response

Posted on 2009-05-10
10
307 Views
Last Modified: 2013-11-18
I need help with the following xml response below:

1) Need code to assign each element as variable->$value (ie. returnCode = $returnCode)
2) Detect if element exists (ie. <olps>). I thought this would work for element detection but it does not.

                   if ( strstr($client->response, "<olps>" ) ) { echo "olps detected"; }

Running PHP 5.1.6 (cli)

Here is the response below in code section when I do:

echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';



HTTP/1.1 100 Continue
Date: Sun, 10 May 2009 18:57:01 GMT
X-Powered-By: ASP.NET
 
 
HTTP/1.1 200 OK
Date: Sun, 10 May 2009 18:57:07 GMT
X-Powered-By: ASP.NET
Connection: close
Transfer-Encoding: chunked
Content-Type: text/xml;charset="utf-8"
X-Powered-By: Servlet/2.5 JSP/2.1
 
 
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:virtualProfile20Response xmlns:ns2="http://www.myurl.com/web-services">
<response>
<correlationData>CD4A07237CC222A</correlationData>
<etailId>0</etailId>
<returnCode>01</returnCode>
<olps>
<prescreenId>874800000000</prescreenId>
<prescreenInd>O</prescreenInd>
<startDate>20090510</startDate>
<expirationDate>20090907</expirationDate>
<purgeDate>20090907</purgeDate>
<creditLimit>2500</creditLimit>
</olps>
</response>
</ns2:virtualProfile20Response>
</S:Body>
</S:Envelope>

Open in new window

0
Comment
Question by:pdheady
[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
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24349631
Are you using nusoap? If you do this:

$result = $client->call(...

... then the $result variable should contain what you need. Do this:

var_dump($result);

Post the result here if you need more help.
0
 
LVL 1

Author Comment

by:pdheady
ID: 24349662
Yes, am using NUSOAP. I did var_dump($result) and it's a TON of data returned in arrays (2 pages). How can i access those elements now? and do my detection?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24349760
Output this first:

var_dump(array_keys($result));

Then you get the array keys on the first level only. Is 'returnCode' and 'olps' there? If not, which keys do you get? If 'olps' is there, you can access data within it like this:

$startDate = $result['olps']['startDate'];
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 1

Author Comment

by:pdheady
ID: 24350385
Yes, they are there.

I'm gettting NULL for both of your examples.

Attached is the output of var_dump($result) which looks messy.
var-dump.txt
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24350412
It looks like this is a dump of the entire client object, not the result... did you do this:

$return = $client->call(... your soap call here );
var_dump($return);

It looks like you did just:

$return = $client;
var_dump($return);

...or

var_dump($client);
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24350450
I wrote "$return" when I meant "$result", sorry if that was confusing. The point is that you must fetch the result of the soap call.
0
 
LVL 1

Author Comment

by:pdheady
ID: 24354317
Looks like that did the trick. Here is the output now:

array(4) { ["correlationData"]=>  string(15) "CD4A08261F24496" ["etailId"]=>  string(1) "0" ["returnCode"]=>  string(2) "01" ["olps"]=>  array(6) { ["prescreenId"]=>  string(12) "874800000000" ["prescreenInd"]=>  string(1) "O" ["startDate"]=>  string(8) "20090511" ["expirationDate"]=>  string(8) "20090908" ["purgeDate"]=>  string(8) "20090908" ["creditLimit"]=>  string(4) "2500" } }

returnCode = 01
olps = Array
OLPS detected!

Let me ask you, if I am calling the $client->call SOAP object earlier in the script then put $result = $client->call(...etc) does this mean I am calling the object twice, hence hitting their web-service twice...?
$result = $client->call('themethodhere', $data, $namespace);
var_dump($result);
 
 $returnCode = $result['returnCode'];
 echo "<br>returnCode = $returnCode";
 
 $olps = $result['olps'];
 echo "<br>olps = $olps";
 
 if ($olps) { echo "<br>OLPS detected!"; }

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24354852
>> does this mean I am calling the object twice, hence hitting their web-service twice...?

Yes. You should probably only do it once.
0
 
LVL 1

Author Comment

by:pdheady
ID: 24355440
Ok thanks.
0
 
LVL 1

Author Closing Comment

by:pdheady
ID: 31579964
Thanks again, excellent help.....much appreciated. Have a good one.
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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

737 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