Matt
Matt

Reputation: 136

Receiving SOAP process with PHP

I have read http://php.net/manual/en/book.soap.php over and over again. I don't understand if i should use SoapServer of SoapClient. It seems that my app should be the server. Trial and error didn't work for me this time.

The situation: a program sends every so many minutes data to the URL of my app. I figured out it receives data when I do echo file_get_contents('php://input'), which seems so stupid to start parsing this with simplexml_load_string(), or whatever (not that I managed).

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope

....

<SOAP-ENV:Body> 

...

    <SOAPSDK1:PostData xmlns:SOAPSDK1="http://tempuri.org/message/">      <tcXML>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;&lt;shop

I think this should be enough code to understand the concept. It is the part after <tcXML>, that I would like to be loaded into an PHP object.

Taking the example from PHP's documentation:

function test($x)
{
    return $x;
}

$server = new SoapServer(null, array('uri' => "http://test-uri/"));
$server->addFunction("test");
$server->handle();

I don't understand what URI I need to provide, since I just receive data?

Then I think: well then I need the SoapClient class, like this:

$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/",
                                     'style'    => SOAP_DOCUMENT,
                                     'use'      => SOAP_LITERAL));

Here I need to supply some uri or location again. I just cannot wrap my mind around it. Are these classes even used for this stuff? Am I asking even the right question here? Any guidance is welcome!

Upvotes: 2

Views: 162

Answers (1)

Kris Peeling
Kris Peeling

Reputation: 1025

When you want to use (or consume) someone elses SOAP web service, you use the SoapClient class.

When you want to expose parts of your app through a web service for others to consume, you use the SoapServer class.

In your situation, another process is sending your app data, so you need to expose part of your application in order to receive that data. So you are right - you could use the SoapServer class.

PHP's built-in SoapServer class will automatically parse the SOAP XML request for you, and turn it into objects you can use in your code. If you find yourself needing to parse XML manually, you are doing something wrong, and you might be better off using a normal XML-RPC style API.

The URI field is essentially used for XML namespacing.

The location parameter in the SoapClient is the web service endpoint. This is useful if you are consuming a SOAP API with multiple endpoints, it allows for specifying the one you want.

Upvotes: 1

Related Questions