【问题标题】:PHP SoapServer - attributes in nodesPHP SoapServer - 节点中的属性
【发布时间】:2016-09-24 20:53:48
【问题描述】:

我已经在其他地方看到过这个问题的回答,但我仍然无法让它发挥作用,所以我需要进一步澄清:

给出的例子是:

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

生成的 xml 将是:

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

但是,我得到了

<tag>
  <_>yyy</_>
  <attr>xxx</attr>
</tag>

那么,是否还需要其他任何东西才能使其按预期工作? SoapServer 类或 WSDL 中的某种配置?

为了让事情更复杂一点,元素是命名空间的,所以实际上我正在寻找一种方法来获取

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

【问题讨论】:

    标签: php xml soap wsdl


    【解决方案1】:

    PHP 肥皂函数太疯狂了,我从来没有发现它有什么问题。我试图通过 SOAP API 将数据连接并更新到 zimbra,并且遇到了很多问题。所以我使用了 SimpleXMLElement & Curl :)

    你可以像这样构建你的 XML:

    $xml = new SimpleXMLElement('<soap></soap>'); // create your base
    
    $xml = $xml->addChild('tag', str_replace('&', '&amp;', 'yyy')); // see addChild in docs
    $xml->attr = 'xxx'; // escaping content rather than addAttribute which does not
    
    echo $xml->asXML(); // which returns : <tag>yyy<attr>xxx</attr></tag>
    

    对于命名空间,addChild中有命名空间参数,但这并没有输出你想要的……

    $xml = $xml->addChild('tag', str_replace('&', '&amp;', 'yyy'), 'ns');
    $xml->attr = 'xxx';
    
    echo $xml->asXML(); // which returns : <tag>yyy<attr>xxx</attr></tag>
    

    PS : 如果你在浏览器中运行,别忘了htmlspecialchars echos :)

    【讨论】:

    • 是的,addAttribute 确实有帮助。不过,我希望有一些更高级别的解决方案。
    • 你的意思是给一个函数一个数组,让它来做东西?
    • 那么你可以使用 FluidXML 来获得更高的 :) github.com/servo-php/fluidxml
    • 感谢您对该库的引用,它似乎很有用。但是,我已经在使用 SoapServer 负责的自动数组到 xml 转换(包括命名空间)的基本功能。对于需要属性的节点,我正在尝试一些奇怪的 SoapVar 东西......无论如何,+1 和 +bounty 的努力。干杯
    猜你喜欢
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2011-01-24
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多