【问题标题】:PHP - Soap Server: get attribute from SOAP requestPHP - Soap 服务器:从 SOAP 请求中获取属性
【发布时间】:2015-03-24 17:38:48
【问题描述】:

我正在运行一个 PHP SoapServer。这是我可以在请求正文中获取的请求示例:

<MyFunctionRequest>
    <Item>
        <Param Attr="XX">Value</Param>
    </Item>
</MyFunctionRequest>

问题是,我似乎无法在我的 SoapServer 函数中读取属性 Attr。如果我打印在 SoapServer 函数中获得的参数,我会得到:

StdClass Object
(
    [Item] => stdClass Object
    (
        [Param] => Value
    )
)

我的 SoapServer 是这样构建的:

<?php
class MySoapServer {
    public function index() {
        $server = new SoapServer($pathToWsdl);
        $server->addFunction(array('MyFunction'));
    }
}

function MyFunction($params) {
    log(print_r($params, true));
    // function code, need the attribute Attr somewhere in here
}

那么如何在我的函数代码中获取属性 Attr 呢?谢谢。

【问题讨论】:

  • 你解决了吗?我也有同样的问题
  • 我最终通过使用$postdata = file_get_contents("php://input"); 将原始请求 XML 作为字符串来解决它,然后我从该字符串中提取了属性。
  • 是的,我也这样做了。

标签: php xml web-services soap


【解决方案1】:

https://stackoverflow.com/a/66821464/372215

我刚遇到这个问题并找到了这个解决方案...

Php Soap 服务器(在后台)不会解析出属性。

使用 DOMDocument 解析数据以获取所有属性

$dom = new \DOMDocument;
$dom->loadXML(file_get_contents("php://input"));

转换成数组方便使用...

What is the best php DOM 2 Array function?

结果

  "MyFunctionRequest" => array:1 [
    "Item" => array:1 [
      "Param" => array:2 [
        "@attributes" => array:1 [
          "Attr" => "XX"
        ]
        "_value" => "Value"
      ]
    ]
  ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多