5

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
goorj
  • 443
  • 7
  • 15

3 Answers3

5

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>
McDowell
  • 107,573
  • 31
  • 204
  • 267
pcmind
  • 990
  • 10
  • 12
  • 2
    you can always refer to php.net :) – pcmind Apr 08 '11 at 12:49
  • 28
    This gives me: `<_>yyyxxx` – mirabilos Nov 04 '15 at 10:25
  • note that you have to be in WSDL-mode to have this work, see here https://stackoverflow.com/questions/24870910/php-soapvar-not-setting-attributes for a good explanation why. In WSDL-mode you don't have to use arrays, this also works for objects! – jfx Nov 08 '17 at 15:41
  • 1
    I am in WSDL-mode and this is not working at all... I get the same result like @mirabilos – h00ligan Jul 09 '21 at 09:08
1

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>
Karthik Murugan
  • 1,429
  • 3
  • 17
  • 28
  • That's good and would be even better with XML parser. You probably don't want to replace `RequestBase` string inside any tag if that happens to be present. – Robo Robok Mar 16 '17 at 10:55
0

pcmind's answer did not work for me, and also not if you try it in PhpFiddle.

I stumbled on this article, which basically creates xml with XMLWriter: http://eosrei.net/articles/2012/01/php-soap-xml-attributes-namespaces-xmlwriter

This totally works for my case.

eddy147
  • 4,853
  • 8
  • 38
  • 57