如何为 PHP 中的 SoapClient 请求向数组添加属性

How Do I Add an Attribute to an Array for a SoapClient Request In PHP

嗨,我对使用 Soap 很陌生,但一切进展顺利,我使用 PHP soap-client 编写了一个脚本,除了一个具有属性的标签外,一切都很好,我可以想办法在调用服务之前将其传递到 $params 列表中。

我是这样构建参数的:- (excert)

'Goods' => array(
      'Description' => '$Description','Quantity' => '$qty');

但是我需要 XML 是:-

<Goods Type="EGoods">              
           <Description>Test</Description>
           <Quantity>1</Quantity>
</Goods>

注意商品标签上的 type="EGoods",我如何修改 php 中的数组以说明此属性?

当我按原样提交 soap 请求时,我收到错误消息说 goods cannot be null or empty。

感谢您的帮助。

在手册中仔细研究之后,我设法找到了一个有效的解决方案:)

'Goods' => array('_' => '', 'Type'=>'EGoods',
        'Description' => '$Description', 'Quantity' => '$qty');

生产

<Goods Type="EGoods">              
       <Description>Test</Description>
       <Quantity>1</Quantity>
</Goods>

在此处找到解决方案 PHP: SoapParam::SoapParam - Manual Comment 114146

我不得不稍微修改它才能工作,如果你将我的数组结构与原始数组结构进行比较,你就会看到我必须进行哪些更改。

保罗

$var = new MessageHeaderSoapStruct;
$var->From    = 'foo';
$var->To      = 'bar';
$var->version = 1;
$namespace = 'http://xxx'; # Namespace of MessageHeader WSDL element
$MessageHeader = new SoapVar($var, SOAP_ENC_OBJECT, 'MessageHeader', $namespace);