php - 如何在php中解析soap xml响应并从字符串中获取信息

标签 php xml soap xml-parsing

我在从 SOAP 响应中提取信息时遇到了一些问题。 这是我得到的回复:

<?xml version="1.0" encoding="utf-8" ?> 
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Body>
- <GetInfoFromSendingResponse xmlns="http://test.test.com/">
  <GetInfoFromSendingResult>{"SendingID":"2468","Subject":"Test","ID":"2468","CampaignID":"890","ForwardAddress":"test@test.ro","SendingTime":"1/14/2016 8:00:00 AM","SendLeadsToEmail":"0","LanguageID":"6","LeadsTestMode":true,"WebversionLink":"","Language":"FR"}</GetInfoFromSendingResult> 
  </GetInfoFromSendingResponse>
  </soap:Body>
  </soap:Envelope>

我需要来自 GetInfoFromSendingResult 的信息并将其存储在一个变量中,这样我就可以使用该信息。

示例:根据 SOAP 响应中提供的 "Language" 信息更改表单的语言。感谢您的帮助。

最佳答案

另一种可能的解决方案是使用例如 SimpleXML .您可以注册命名空间并使用 xpath表达式:

$source = <<<SOURCE
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetInfoFromSendingResponse xmlns="http://test.test.com/">
            <GetInfoFromSendingResult>{"SendingID":"2468","Subject":"Test","ID":"2468","CampaignID":"890","ForwardAddress":"test@test.ro","SendingTime":"1/14/2016 8:00:00 AM","SendLeadsToEmail":"0","LanguageID":"6","LeadsTestMode":true,"WebversionLink":"","Language":"FR"}</GetInfoFromSendingResult>
        </GetInfoFromSendingResponse>
    </soap:Body>
</soap:Envelope>
SOURCE;

$xml = simplexml_load_string($source);
$xml->registerXPathNamespace('test', 'http://test.test.com/');
$elements = $xml->xpath('//soap:Envelope/soap:Body/test:GetInfoFromSendingResponse/test:GetInfoFromSendingResult');
$result = json_decode($elements[0], true);
print_r($result);

将导致:

Array
(
    [SendingID] => 2468
    [Subject] => Test
    [ID] => 2468
    [CampaignID] => 890
    [ForwardAddress] => test@test.ro
    [SendingTime] => 1/14/2016 8:00:00 AM
    [SendLeadsToEmail] => 0
    [LanguageID] => 6
    [LeadsTestMode] => 1
    [WebversionLink] => 
    [Language] => FR
)

关于php - 如何在php中解析soap xml响应并从字符串中获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35080130/

相关文章:

android - 在多个 textView 中设置 textColor

node.js - 简单的 SOAP 调用失败(Nodejs,easysoap)

.net - erlang 中的 SOAP 网络服务

php - 静态页面cakephp的授权

php - PDO "SELECT"没有返回结果

php - 使用php mysql分页遇到困难

PHP - 从 SOAP 响应中解析数据

php - Codeigniter 数据库 Where 有多个子句,另一个 where

javascript - Nodejs 从嵌套函数返回结果

java - 解析 XML 文件时如何忽略空格和换行符