繁体   English   中英

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

[英]How to parse soap xml response in php and get the information from the string

我在从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"信息来更改表单的"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 5.0及更高版本随附的SoapClient

$client = new SoapClient("http://test.test.com/?wsdl");
$res = $client->SoapFunction(array('param1'=>'value','param2'=>'value'));
echo $res->GetInfoFromSendingResponse->GetInfoFromSendingResult;

然后,您可能需要JSON解码才能在其中获取某些特定值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM