Jump to content

Soap Client call returning XML string as just a string without formatting


Recommended Posts

Hello,

 

I am quite new to PHP so am still finding my way around so this may have a really obvious answer, but my searches so far have not given me the answer.

 

I have a need to connect to two web services and am using the SoapClient function. One of the web services is working fine as it returns the data as an object which I am able to use "$result->

 -> [Required value from data];" and it returns the value from the XML as expected.

The code I am using is, minus the values as they are user tokens, etc.

[code=php:0]
$client = new SoapClient("Web Service URL");

$result = $client->GetHistory(array("Parameter1" => "Value","Parameter2" => "Value"));

print_r($result);
[/code]

 

However the other web service returns the data as a string of XML:

 

<?xml version="1.0"?><tag1><tag2>Data</tag2><tag2>Data</tag2><tag3>Data</tag3></tag1>

 

The problem is that when returned if I output using print_r($result) to see what is contained all of the tags have been removed including the "<?xml version="1.0"?>". This makes the data unusable as it is just a long string now with no seperators to identify the seperation of the data.

 

Why is this happening? What have I missed?

 

Any help would be much appreciated.

 

Thanks,

 

Lee

If you want to see the result for debugging:

 

print htmlentities($result);

 

Or you may need to display some element from $result if that XML is not at the top level.

 

If you want to use the result in your code, you will need to use an XML parser.

 

Is it possible the server is sending XML encapsulated in SOAP?  So the SOAP library has already stripped all the SOAP container, and what you're left with is the data, which also happens to be XML itself?

Hello,

 

I tried that but no joy.

 

This is the code that I am using, have set up a test web service so you can check.

 

<?php

try {
    $client = new SoapClient("http://webservice.lhldev.net/testservice.asmx?wsdl");

    $result = $client->TestString();
    
    print_r($result);

} catch (Exception $e) {
    echo "Error!<br />";
    echo $e -> getMessage ();
}

?>

 

Returns:

 

stdClass Object ( [TestStringResult] => ABCDE123456789Test Text 1Test Text 1123456123456 )

 

 

The web service code is:

 

[WebMethod]
    public string TestString()
    {
        return "<information><MainA>ABCDE</MainA><MainB>123456789</MainB><SetA><ValuesA><TextLine1>Test Text 1</TextLine1><TextLine2>Test Text 1</TextLine2></ValuesA></SetA><SetB><ValuesB><NumberLine1>123456</NumberLine1><NumberLine2>123456</NumberLine2></ValuesB></SetB></information>";
    }

 

As you can see the XML formatting is being stripped out, however if you run the web service directly then string is shown fully so I suspect somethign in the PHP Soap Client is removing the XML formatting.

 

Obviously having a XML string returned as a string is not ideal but the web service I am using is 3rd party and out of my control so I need a way to use the data. Current;y the string is sent to a MS-SQL server proc and the string is then converted to XML and placed in a table. The data can then be read and used for a comparasion agaisnt the server.

 

Thanks again,

 

Lee

Just noticed that if I use the following code:

 

$client = new SoapClient("http://webservice.lhldev.net/testservice.asmx?wsdl", array('trace' => 1));

$result = $client->TestString();
    
echo $client->__getLastResponse();

 

Then I get the following response:

 

<information><MainA>ABCDE</MainA><MainB>123456789</MainB><SetA><ValuesA><TextLine1>Test Text 1</TextLine1><TextLine2>Test Text 1</TextLine2></ValuesA></SetA><SetB><ValuesB><NumberLine1>123456</NumberLine1><NumberLine2>123456</NumberLine2></ValuesB></SetB></information>

 

Which is what I would expect so it seems that when storing the $client to a $result variable the XML is stripped out and I am left with just the data.

 

Am I calling the web service incorrectly in the first case?

 

Lee

I don't see anything wrong with how you are calling the service.  At this stage I would follow one of two approaches:

 

1.  Just use __getLastResponse() and forget about $result

2.  Look at the soap client library and see where the XML is getting stripped.  Mine can be found here: /usr/share/php/SOAP/Client.php.  I'm also curious why there is no xml header on __getLastResponse() - shouldn't there be one?

 

Can you confirm for me that you're looking at the raw output of print_r(), and not viewing it in a web browser which is trying to render it as HTML?  I'm asking because trying to display XML as HTML usually gives you the data without the tags.

Unless you've done something on the service side, the answer would appear to be btherl's here:

Can you confirm for me that you're looking at the raw output of print_r(), and not viewing it in a web browser which is trying to render it as HTML?  I'm asking because trying to display XML as HTML usually gives you the data without the tags.

 

Running your test script from the command line gives me

stdClass Object
(
    [TestStringResult] => <information><MainA>ABCDE</MainA><MainB>123456789</MainB><SetA><ValuesA><TextLine1>Test Text 1</TextLine1><TextLine2>Test Text 1</TextLine2></ValuesA></SetA><SetB><ValuesB><NumberLine1>123456</NumberLine1><NumberLine2>123456</NumberLine2></ValuesB></SetB></information>
)

Hello,

 

I just checked the source in the browser and the tags are there so it is returning the data correctly. Should have checked that first as I have been used to working with .NET which returns the data as is and doesn't strip the tags out when rendering in the browser.

 

As long as the full string is returned that is fine as it is never retunred to the browser. I can now send it to MySQL to populate a table in a Stored Proc to use. I am just looking at the possability of switching to PHP and MySQL from VBScript and .NET and MS-SQL.

 

Out of interest is there any reason why the tags are shown when using _getLastResponse? Is it rendered differently when shown in the browser?

 

As for the missing XML start tag that was in the web service, copied code from an existing web service I used which strips the XML tag out. It is back in now but shouldn't make a difference now.

 

Thank you for all the help, I should have probablly checked the source of the page first but like I said been used to .NET which seems to render the output as is.

 

Lee

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.