Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add attributes to soapVars

Tags:

php

soap

I'd like to create soapVars with attributes like this:

<tag attr="xxx">yyy</tag>

Is this possible with the SoapVar constructor, but without using XSD_ANYXML and raw xml strings?

like image 477
goorj Avatar asked Feb 13 '10 15:02

goorj


2 Answers

The best way to do it is:

<?php 
 $tag['_'] = 'yyy'; 
 $tag['attr'] = 'xxx'; 
 $tagVar = new SoapVar($tag, SOAP_ENC_OBJECT); 

?> 

the result would be:

<tag attr="xxx">yyy</tag>
like image 118
pcmind Avatar answered Oct 06 '22 00:10

pcmind


After spending many hours searching for a solution, I only found this workaround. Works in my case.

/**
 * A SoapClient derived class that sets the namespace correctly in the input parameters
 */

class SoapClientNS extends SoapClient {
// return xml request
function __doRequest($request, $location, $action, $version, $one_way = NULL) {

    //Replace each <Typename> with <ns1:Typename> or 
    $request = str_replace('RequestBase', 'ns1:RequestBase', $request);

    return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}

$client = new SoapClientNS($wsdlURL);
$client->getAllBooks(array('RequestBase' => array('code' => 'AAAA', 'password' => '234234fdf')));

The request XML was something like this:

    <?xml version="1.0" encoding="UTF-8"?>

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.travco.co.uk/trlink/xsd/country/request">
<env:Body>
<ns1:getAllBooks>
<RequestBase code="AAAA" password="234234fdf"/>
</ns1:getAllBooks>
</env:Body>
</env:Envelope>
like image 36
Karthik Murugan Avatar answered Oct 06 '22 01:10

Karthik Murugan