【问题标题】:Creating a SOAP request with PHP - how do I add attributes to the XML tags?使用 PHP 创建 SOAP 请求 - 如何向 XML 标记添加属性?
【发布时间】:2010-11-27 01:55:11
【问题描述】:

我正在尝试在我的 SOAP 调用中生成以下 XML:

<CResponse>
    <ID>int</ID>
    <Response Action="Apply">
        <Question ID="$someint">
            <Responses>
                <Response ID="$someotherint" />
                <Response ID="$yetanotherint" />
            </Responses>
        </Question>
    </Response>
</CResponse>

我可以很好地创建大部分调用 - 我终于知道嵌套数组是我的朋友 - 但我不知道如何将这些 ID="$int"Action="Apply" 属性添加到各种标签。我敢肯定这很容易,但我就是想不通。

TIA。

【问题讨论】:

  • 你是如何创建 XML 的?
  • 只是将它作为参数传递给我的 SOAP 函数调用。所以,假设 $client 是我的 SOAP 句柄,它是 $result = $client->NameOfFunction(array(whatever => whatever));
  • $client 到底是什么?是uk.php.net/manual/en/class.soapclient.php 吗?
  • 这个问题被转发了,见stackoverflow.com/questions/1408748/…
  • @benjy:您不应该立即重新发布未解决的问题,如果出现新问题,只需编辑/增强原始问题。我在这里回答,因为这似乎是原始问题,而且标题更好。

标签: php xml soap


【解决方案1】:

您应该能够使用以下语法添加属性:

array("foo" => array("_" => "cheese", "bar"=>"moo"));

这应该产生以下 XML

<foo bar="moo">cheese</foo>

【讨论】:

  • 如果我有这样的 XML &lt;foo bar="moo"&gt;&lt;bar&gt;moo2&lt;/bar&gt;&lt;/foo&gt; 怎么办?
  • 参数的顺序很重要。 “_”应该是第一个。
【解决方案2】:

我不知道 JJ 建议的(有些奇怪的)语法(根据您对问题的重新发布,它似乎不起作用),但是您应该能够以任何方式塑造您的请求 xml通过或多或少“手动”构造它使用SoapVar 类和XSD_ANYXML 编码。

有关如何执行此操作的示例,请参阅 this user comment to the SoapVar Constructor documentation

如果您需要构建的 XML 变得更复杂,请考虑使用 SimpleXML 或 DOMDocument 来组装它,而不是直接编写它。

(我觉得应该没有更简单的方法来做到这一点,但到目前为止我还没有找到。)


编辑:刚刚在this question asking for a simpler way to do it 中找到了另一个使用 XSD_ANYXML 编码的示例。不幸的是,到目前为止没有人想出一种更简单的方法:/

【讨论】:

    【解决方案3】:

    我在属性方面遇到了同样的问题,我最终使用了 XMLWriter,正如这篇文章中所评论的那样:http://eosrei.net/articles/2012/01/php-soap-xml-attributes-namespaces-xmlwriter

    构建请愿书:

    $prefix = 'ns1';
    
    $xml = new \XMLWriter();
    $xml->openMemory();
    
    $xml->startElementNs($prefix, 'SomeRequest', null);
        $xml->writeElementNs($prefix, 'SchemaVersion', null, "2.0");
        $xml->startElementNs($prefix, 'SomeComplexType', null);
            $xml->writeElementNs($prefix, 'MessageId', null, 11);
            $xml->writeElementNs($prefix, 'Timestamp', null, '2013-07-05T14:43:43.649-04:00');
            $xml->startElementNs($prefix, 'Authentication', null);
                $xml->writeAttribute('SomeAttribute', '12312');
                $xml->writeElementNs($prefix, 'UserId', null, 'myUser');
                $xml->writeElementNs($prefix, 'Password', null, 'somePass');
            $xml->endElement();
        $xml->endElement();
    $xml->endElement();
    
    $request = new SoapVar($xml->outputMemory(), XSD_ANYXML);
    
    $result = $this->call('submit', $request);
    

    结果:

    <?xml version="1.0" encoding="UTF-8"?> 
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://xml.somehost.com/XMLSchema/Connect">
       <SOAP-ENV:Body>
          <ns1:SomeRequest>
             <ns1:SchemaVersion>2.0</ns1:SchemaVersion>
             <ns1:SomeComplexType>
                <ns1:MessageId>11</ns1:MessageId>
                <ns1:Timestamp>2013-07-05T14:43:43.649-04:00</ns1:Timestamp>
                <ns1:Authentication SomeAttribute='12312'>
                   <ns1:UserId>myUser</ns1:UserId>
                   <ns1:Password>somePass</ns1:Password>
                </ns1:Authentication>
             </ns1:SomeComplexType>
          </ns1:SomeRequest>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多