doujian3401 2013-05-15 04:24
浏览 106
已采纳

解析CURL SOAP响应

I am accessing a SOAP server via CURL (Its the only way that PHP would connect). This is the response that I'm receiving:

<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>

I'm trying to pull the content of:

<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>

But I can't figure out how to parse it. I've tried "simplexml_load_string" put that throws a bunch of errors such as "namespace warning : xmlns: URI PublicApi is not absolute"

Any Suggestions?

  • 写回答

1条回答 默认 最新

  • douyi1197 2013-05-15 04:44
    关注

    A bit of a hack, but if the format of the response is always roughly the same, the following might work for you:

    ?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]."
    ";
    preg_match($reg2, $msg, $m);
    print "result: ".$m[1]."
    ";
    ?>
    

    Result of the above:

    message: Successfully completed the operation
    result: 0
    

    Explanation:

    [^>]*> : "any number of characters that are NOT >, followed by >

    ([^<]*) : "'capture' all characters that are NOT <. return them in $m[1]

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?