Archive for November, 2007

PHP/Soap Arrays with Single Elements

Tuesday, November 13th, 2007

I’m sure you have run into this before. if an Element is defined in the Soap WSDL and a single element is returned - it is treated as an object in the Soap Results, and not an array.

Turns out there is a quick fix:

  $x = new SoapClient($wsdl, array(’features’ =>
SOAP_SINGLE_ELEMENT_ARRAYS));

Thats it. If a single element is returned, PHP will handle this is a single element array, and not as an object! Handy to avoid all those if( is_object())   elseif( is_array()) constructs in your code!

Soap Headers with PHP Inbuilt Soap Toolkit

Tuesday, November 13th, 2007

Soap Headers are great, and the inbuilt Soap Library in PHP 5 allows you to add headers very easily:

$Headers[] = new SoapHeader( ‘http://www.test.com/test/v2′,
‘header_name’,
‘header value’,
false,
‘http://schemas.xmlsoap.org/soap/actor/next’ );
$Client = new SoapClient( $URL );
try {
$Response = $Client->__call(’methodname’, $Params, null, $Headers );

But, its a bit ugly. Particularly when using the __call() method to call the Soap method. There has to be a better way.Well of course there it. After all - this is PHP not some sort of communist language like Java :P

$Client->__setSoapHeaders($Headers );
try {
$Response = $Client->methodname( $Params )

The trick is to use the “__setSoapHeaders” function to set the headers - that way you can simply call the method on the Soap Client as per normal.

__setSoapHeaders is not in the online PHP Documentation - however it appears to work in all versions of PHP 5.X