views:

454

answers:

2

Hi,

I need to convert this .NET syntax into PHP using SoapHeader() call.

esb.RequestServerVersionValue = new RequestServerVersion(); esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;

Thanks a lot! :)

A: 

I personally never managed to get the headers the way I wanted them when using the SoapHeader class. To be more flexible you should perhaps take a custom SoapClient class into consideration. As I answered in another question on SO you can structure the SOAP message to your needs when overriding SoapClient::__doRequest(). That way you can insert XML fragments at will.

class My_SoapClient extends SoapClient
{
    protected function __doRequest($request, $location, $action, $version) 
    {
        /*
         * $request is a XML string representation of the SOAP request
         * that can e.g. be loaded into a DomDocument to make it modifiable.
         */
        $domRequest = new DOMDocument();
        $domRequest->loadXML($request);

        // modify XML using the DOM API, e.g. get the <s:Header>-tag 
        // and add your custom headers
        $xp = new DOMXPath($domRequest);
        $xp->registerNamespace('s', 'http://www.w3.org/2003/05/soap-envelope');
        $headers = $xp->query('/s:Envelope/s:Header');
        if ($headers->length == 0) {
            $envelope = $xp->query('/s:Envelope')->item(0);
            $header = $domRequest->createElementNS('http://www.w3.org/2003/05/soap-envelope', 's:Header');
            $envelope->appendChild($header);
        } else {
            $header = $headers->item(0);
        }

        // now add your custom header
        $requestServerVersion = $domRequest->createElementNS('T_NAMSPACE', 't:RequestServerVersion');
        $requestServerVersion->setAttribute('Version', 'Exchange2007_SP1');

        $header->appendChild($requestServerVersion);

        $request = $domRequest->saveXML();
        return parent::__doRequest($request, $location, $action, $version);
    }
}

T_NAMSPACE must be changed to the correct namespace of prefix t.

Stefan Gehrig
That looks terrific. Thanks for sharing. I also figured out how to solve this problem and a hundred ways to get the wrong result. Everything is simple in hindsight. :)
A: 
$soapHeader = new SoapHeader(
                     'http://schemas.microsoft.com/exchange/services/2006/types',
                     'RequestServerVersion Version="Exchange2007_SP1"'
                     );

$client->__setSoapHeaders($soapHeader);

This is basically all that really needed to be set. I got confused with namespace settings. Curiously, RequestServerVersion header is required when working with public folders, but does not appear to be required when working with mailbox items in Exchange 2007.

This link was particularly helpful: http://www.zimbra.com/forums/developers/5532-php-soap-vs-zimbra.html as it showed me how to enable debugging and made it very clear what each attribute did.

This Google search result shows valid XML necessary to generate for this to work "t:RequestServerVersion"