tags:

views:

372

answers:

1

Given that the following client.php creates this request XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://soap.dev/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
  <SOAP-ENV:Body>
    <ns1:Test>
      <RequestId xsi:type="xsd:int">1</RequestId>
      <PartnerId xsi:type="xsd:int">99</PartnerId>
    </ns1:Test>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

How do I access the names of the parameters? (RequestId and PartnerId) inside a server.php? The names are clearly there in the payload, but on server side only the values are received (1 and 99)

Sample code follows:

client.php

<?php
$client_params = array(
    'location' => 'http://soap.dev/server.php',
    'uri' => 'http://soap.dev/',
    'trace' => 1
);

$client = new SoapClient(null, $client_params);

try {
    $res = $client->Test(
        new SoapParam(1, 'RequestId'),
        new SoapParam(99, 'PartnerId')
    );

} catch (Exception $Ex) {
    print $Ex->getMessage();
}

var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());

server.php

class receiver {
    public function __call ($name, $params)
    {
        $args = func_get_args();
        // here $params equals to array(1, 99)
        // I want the names as well.
        return var_export($args, 1);
    }

}

$server_options = array('uri' => 'http://soap.dev/');
$server = new SoapServer(null, $server_options);
$server->setClass('receiver');
$server->handle();

Please note that I can not really change the incoming request format.

Also, I am aware that I could give the names back to parameters by creating a Test function with $RequestId and $PartnerId parameters.

But what I really want to is to get name/value pairs out of incoming request.

So far the only idea I have is to simply parse the XML and this cannot be right.

+1  A: 

Back when I had this problem I finally decided to go with the proxy function idea - Test function with parameters named ($RequestId and $PartnerId) to give names back to parameters. Its adequate solution, but certainly not the best.

I have not yet lost the hope for finding a better solution though, and here is my best idea so far

<?php
class Receiver {

    private $data;

    public function Test ()
    {
        return var_export($this->data, 1);
    }

    public function int ($xml)
    {
        // receives the following string
        // <PartnerId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:int">99</PartnerId>
        $element = simplexml_load_string($xml);
        $this->data[$element->getName()] = (string)$element;
    } 

}

$Receiver = new Receiver();

$int = array( 
    'type_name' => 'int'
    , 'type_ns' => 'http://www.w3.org/2001/XMLSchema'
    , 'from_xml' => array($Receiver, 'int')
);


$server_options = array('uri' => 'http://www.w3.org/2001/XMLSchema', 'typemap' => array($int), 'actor' => 'http://www.w3.org/2001/XMLSchema');
$server = new SoapServer(null, $server_options);
$server->setObject($Receiver);
$server->handle();

It still means parsing XML manually, but one element at a time which is a bit more sane that parsing entire incoming SOAP message.

Anti Veeranna