Jump to content

SOAP Returned Values


sanchez77

Recommended Posts

So I am accessing a web service and it returns a bunch of values, I am able to grab and print the first value, but I can't figure out how to print the second value.

 

PHP Code that works

 

<?php
//Web service that works fine
$client = new SoapClient("http://www.domain.com/sc001.asmx?WSDL");  
$sc001 = $client->$NT(array('var1'=>$var1));

//Grab the value tag and print it from web service call and in this case prints 001
$tag = $sc001->NTResult->results->TaggedText->tag;
echo $tag;
?>

 

But how do I get it to print the second tag that equals 120?

 

I've tried this

 

<?php
$tag2 = $sc001->NTResult->taggedResults->tag;
?>

 

and this

 

<?php
$tag2 = $sc001->NTResult->taggedResults->TaggedText->tag;
?>

 

and this

 

<?php
$tag2 = $sc001->NTResult->results->TaggedText->taggedResults->TaggedText->tag;
?>

 

But neither work. The var_dump for is sc001 is:

 

object(stdClass)#9 (1) { ["NTResult"]=> object(stdClass)#10 (2) { ["count"]=> int(1) ["results"]=> object(stdClass)#11 (1) { ["TaggedText"]=> object(stdClass)#12 (2) { ["tag"]=> string(3) "001" ["taggedResults"]=> object(stdClass)#13 (1) { ["TaggedText"]=> array(27) { [0]=> object(stdClass)#14 (2) { ["tag"]=> string(4) "120" ["textArray"]=> object(stdClass)#15 (1) { ["string"]=> array(2) { [0]=> string(28) "Rocks " [1]=> string(14) "Big Rocks" } } }  } } } } } } } } 

 

and the XML return from the service is:

 

  <?xml version="1.0" encoding="utf-8" ?> 
- <TaggedTextArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.domain.com/sc001">
  <count>1</count> 
- <results>
- <TaggedText>
  <tag>001</tag> 
- <taggedResults>
- <TaggedText>
  <tag>120</tag> 
- <textArray>
  <string>Rocks</string> 
  <string>Big Rocks</string> 
  </textArray>
  </TaggedText>

 

Maybe the question is, how do you handle a SOAP returned array? I'm not sure, any help is appreciated.

 

Thanks,

sanchez

 

Link to comment
Share on other sites

Thanks but that didn't work.

 

it's weird, I am able to capture the values in the first TaggedText section (tag=001), but I can't seem to get to the second set of Tagged Text, in particular tag=120.

 

The XML return from the SOAP call is:

 

- <TaggedTextArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.domain.com/sc001">
  <count>1</count> 
- <results>
     - <TaggedText>
            <tag>001</tag> 
          - <taggedResults>
               - <TaggedText>
                 <tag>120</tag> 
               - <textArray>
                    <string>value1</string> 
                    <string>value2</string> 
                 </textArray>
                </TaggedText>
             </taggedResults>
        </TaggedText>
</results>

 

Any other ideas out there?

 

Thanks,

sanchez

Link to comment
Share on other sites

I can't give you tested code, as I have no access to the SOAP server.

 

If you can get the raw XML string, though (http://www.php.net/manual/en/soapclient.getlastresponse.php) you can use SimpleXML to easily parse the results.

 

From what I've tried though, it appears your SOAP call isn't returning valid XML.

<?php

$xml_data = '<?xml version="1.0" encoding="utf-8" ?>
<TaggedTextArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.domain.com/sc001">
<count>1</count>
<results>
     <TaggedText>
            <tag>001</tag>
          <taggedResults>
               <TaggedText>
                 <tag>120</tag>
               <textArray>
                    <string>value1</string>
                    <string>value2</string>
                 </textArray>
                </TaggedText>
             </taggedResults>
        </TaggedText>
</results>';

try {
$xml = new SimpleXMLElement( $xml_data, LIBXML_NSCLEAN );
print_r( $xml );
} catch (Exception $e) {
echo 'Caught exception: '.$e->getMessage();
}

?>

 

Returns a bunch of errors about the XML data, and throws an exception.

This can be fixed by removing the first three lines, but it's kinda 'hackish'

 

<?php

$xml_data = '<?xml version="1.0" encoding="utf-8" ?>
<TaggedTextArray xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.domain.com/sc001">
<count>1</count>
<results>
     <TaggedText>
            <tag>001</tag>
          <taggedResults>
               <TaggedText>
                 <tag>120</tag>
               <textArray>
                    <string>value1</string>
                    <string>value2</string>
                 </textArray>
                </TaggedText>
             </taggedResults>
        </TaggedText>
</results>';

try {
$xml_data = explode( "\n", $xml_data );
$xml_data = array_slice( $xml_data, 3 );
$xml_data = implode( '', $xml_data );
$xml = new SimpleXMLElement( $xml_data, LIBXML_NSCLEAN );
print_r( $xml );
foreach( $xml->TaggedText->taggedResults->TaggedText->textArray->string as $string ) {
	echo '<br><b>'.$string.'</b>';
}
} catch (Exception $e) {
echo 'Caught exception: '.$e->getMessage();
}

?>

 

Returns

 

SimpleXMLElement Object
(
    [TaggedText] => SimpleXMLElement Object
        (
            [tag] => 001
            [taggedResults] => SimpleXMLElement Object
                (
                    [TaggedText] => SimpleXMLElement Object
                        (
                            [tag] => 120
                            [textArray] => SimpleXMLElement Object
                                (
                                    [string] => Array
                                        (
                                            [0] => value1
                                            [1] => value2
                                        )

                                )

                        )

                )

        )

)
<br><b>value1</b><br><b>value2</b>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.