3

I am building a web service using NuSoap library in PHP. My webservice will act as a middle layer between client and an already existing web service by a vendor. So instead of client connecting to the vendor directly, they will connect to my web service, my web service connects to the vendor and grab the response and send the same response back to the client.

My only problem is my vendor is sending back stdclass object (their webservice is written in .Net) and I have to receive that object and send back the same object to the client on my webservice method.

I have searched quite a bit on Internet but there are no clear ways how to do acheive this by NuSoap library. Whatever I have read so far, specify that I have to use complex type to acheive this, but again I have no clue how to grab the response and then convert it to the complex type and send it back to the client.

Thanks in advance for your help.

Ricky
  • 43
  • 1
  • 4
  • 9

1 Answers1

2

What you are writing is referred to as a proxy.

There are some examples online for NuSoap server sending complex types through the addComplexType method.

//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string')));

One approach to implementing the proxy, is build your service with stubbed out data, such that it doesn't actually talk to the backend service first. See if you can get the original client satisfied with a contrived response from your proxy. Then once you have that, consuming the real backend service should be trivial (SOAP client operations are easier than server ones in my experience).

Another alternative is to consider the native SoapServer class instead. The first comment here shows how to create a complex type.

EDIT

After looking around a bit more, here is a much better example.

There are 2 ways to register a complex type with NuSoap, per the docblock on addComplextType (lib/class.wsdl.php)

/**  
* adds an XML Schema complex type to the WSDL types
*
* @param string $name
* @param string $typeClass (complexType|simpleType|attribute)
* @param string $phpType currently supported are array and struct (php assoc array)
* @param string $compositor (all|sequence|choice)
* @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
* @param array $elements e.g. array ( name => array(name=>'',type=>'') )
* @param array $attrs e.g. array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'xsd:string[]'))
* @param string $arrayType as namespace:name (xsd:string)
* @see nusoap_xmlschema
* @access public
*/

See how he does it on the later example I posted:

$server->wsdl->addComplexType('Contact',
    'complexType',
    'struct',
    'all',
    '',
    array(
            'id' => array('name' => 'id', 'type' => 'xsd:int'),
            'first_name' => array('name' => 'first_name', 'type' => 'xsd:string'),
            'last_name' => array('name' => 'last_name', 'type' => 'xsd:string'),
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'phone_number' => array('name' => 'phone_number', 'type' => 'xsd:string')
    )
);

Then how to return the response with the Contact complex type:

function updateContact($in_contact) {
    $contact = new Contact($in_contact['id']);
    $contact->first_name=mysql_real_escape_string($in_contact['first_name']);
    $contact->last_name=mysql_real_escape_string($in_contact['last_name']);
    $contact->email=mysql_real_escape_string($in_contact['email']);
    $contact->phone_number=mysql_real_escape_string($in_contact['phone_number']);
    if ($contact->update()) return true;
}

You can also see how to use the array variant in his example. Sorry for the huge answer!

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • Thanks for the response, regarding the stubbed data, we always want to send back the latest response from server which means even if the vendor API has changed or gives back a different output our API should also reflect the same realtime. So stubbed data might be difficult to use in this situation. Coming to the complextype example for nusoap, Do I need to parse the object coming from the server and then reassign it in the array to build a complextype? Also can I build a complextype inside a method in my API or it has to be outside the method? – Ricky Dec 17 '13 at 16:53
  • Ah, that could be a bit painful to implement, but I hear you on the stubbed data. For the complex type, you want to register the complex type, then in the method that handles the request (`updateContact` in the second example above) the client will send you want to use a `soapclient` to connect to the backend service. Parse the response from it, create the object or array (based on `struct` or `array` option in `addComplexType`, then return that from the handler. – quickshiftin Dec 17 '13 at 17:27