解析CURL SOAP响应

时间:2013-05-15 04:24:29

标签: php soap curl

我通过CURL访问SOAP服务器(它是PHP连接的唯一方式)。这是我收到的回复:

<s:envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:header>
    <a:action s:mustunderstand="1">PublicApi/IPropertyService/CreatePropertyResponse</a:action>
</s:header>
<s:body>
    <createpropertyresponse xmlns="PublicApi">
        <createpropertyresult xmlns:b="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi.Property" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <message xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">Successfully completed the operation</message>
            <result xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">0</result>
            <transactiondate xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">2013-05-15T04:07:48.6565312Z</transactiondate>
            <b:propertyid>55</b:propertyid>
        </createpropertyresult>
    </createpropertyresponse>
</s:body>

我试图提取以下内容:

<message xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">Successfully completed the operation</message>
<result xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">0</result>
<b:propertyid>55</b:propertyid>

但我无法弄清楚如何解析它。我试过“simplexml_load_string”put抛出了一堆错误,比如“命名空间警告:xmlns:URI PublicApi不是绝对的”

任何建议?

1 个答案:

答案 0 :(得分:2)

有点骇客,但如果响应的格式总是大致相同,以下内容可能对您有用:

?php
// the message would come from somewhere else; I hard code it to test the expression that follows:
$msg='<s:envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:header>
    <a:action s:mustunderstand="1">PublicApi/IPropertyService/CreatePropertyResponse</a:action>
</s:header>
<s:body>
    <createpropertyresponse xmlns="PublicApi">
        <createpropertyresult xmlns:b="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi.Property" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <message xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">Successfully completed the operation</message>
            <result xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">0</result>
            <transactiondate xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">2013-05-15T04:07:48.6565312Z</transactiondate>
            <b:propertyid>55</b:propertyid>
        </createpropertyresult>
    </createpropertyresponse>
</s:body>';

// here comes the actual parsing:
$reg1='/<message [^>]*>([^<]*)</';
$reg2='/<result [^>]*>([^<]*)</';
preg_match($reg1, $msg, $m);
print "message: ". $m[1]."\n";
preg_match($reg2, $msg, $m);
print "result: ".$m[1]."\n";
?>

上述结果:

message: Successfully completed the operation
result: 0

说明:

[^>]*>:“任意数量的字符不是>,后跟>

([^<]*):“'捕获'所有非<的字符。\ n返回$m[1]

我希望这会有所帮助。