【问题标题】:PHP - SOAP response to JSONPHP - 对 JSON 的 SOAP 响应
【发布时间】:2015-01-22 15:24:17
【问题描述】:

我正在尝试将 SOAP 响应解析为 JSON。到目前为止,我有这个代码:

$data = '<?xml version="1.0" encoding="utf-8"?>';
    $data .= '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
    $data .= '<soap12:Body>';
    $data .= '<GetCommunities xmlns="url">';
    $data .= '<APIUsername>string</APIUsername>';
    $data .= '<APIPassword>string</APIPassword>';
    $data .= '</GetCommunities>';
    $data .= '</soap12:Body>';
    $data .= '</soap12:Envelope>';

    $soap_do = curl_init();
    curl_setopt($soap_do, CURLOPT_URL,            "url" );
    curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($soap_do, CURLOPT_TIMEOUT,        10);
    curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($soap_do, CURLOPT_POST,           true );
    curl_setopt($soap_do, CURLOPT_POSTFIELDS,     $data);
    curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($data) ));

    $result = curl_exec($soap_do);
echo $result;

此代码正在运行,我得到以下结果:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetCommunitiesResponse xmlns="url">
            <GetCommunitiesResult>
                <Communities>
                    <CommunityID>1</CommunityID>
                    <CommunityName> Not Specified</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>276</CommunityID>
                    <CommunityName>Bella Toscana</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>31</CommunityID>
                    <CommunityName>Crescent Lakes</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>62</CommunityID>
                    <CommunityName>Hillcrest Estate</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>750</CommunityID>
                    <CommunityName>Sunny Beach</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>124</CommunityID>
                    <CommunityName>Terra Verde Resort</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>744</CommunityID>
                    <CommunityName>The Dales at West Haven</CommunityName>
                </Communities>
                <Communities>
                    <CommunityID>158</CommunityID>
                    <CommunityName>Westridge</CommunityName>
                </Communities>
            </GetCommunitiesResult>
        </GetCommunitiesResponse>
    </soap:Body>
</soap:Envelope>

我需要根据 GetCommunitiesResult 标记之间的数据创建 JSON 响应。我该怎么做?

【问题讨论】:

  • 问题到底出在哪里?遍历 XML 并在此过程中创建相应的对象。使用json_encode() 序列化该对象,您就完成了。
  • 如何遍历 XML :D?

标签: php json soap


【解决方案1】:

已编辑并tested

// a bit of a hack, but let's see...
list($trash,$result)=explode('<soap:Body>',$result);
list($result,$trash)=explode('</soap:Body>',$result);
unset($trash);
$result=str_replace('xmlns="url"','',$result);

$simple_result=simplexml_load_string($result);
$json_result=json_encode($simple_result);

//var_export($simple_result);
echo $json_result;

输出:

{"GetCommunitiesResult":{"Communities":[{"CommunityID":"1","CommunityName":" 未指定"},{"CommunityID":"276","CommunityName":"Bella 托斯卡纳"},{"CommunityID":"31","CommunityName":"Crescent 湖泊"},{"CommunityID":"62","CommunityName":"Hillcrest 庄园"},{"CommunityID":"750","CommunityName":"Sunny 海滩"},{"CommunityID":"124","CommunityName":"Terra Verde 度假村"},{"CommunityID":"744","CommunityName":"西部山谷 避风港"},{"CommunityID":"158","CommunityName":"Westridge"}]}}

【讨论】:

  • 这给了我一个空对象。
  • 错字$simple_result vs $simlpe_result
  • 我只是在回顾自己的想法并试图确定这是否是一个好的答案。让我测试一下,看看我想出了什么......
  • 好的,还不行。让我调查一下。 Warning: simplexml_load_string(): namespace warning : xmlns: URI url is not absolute in ...
  • 好的,因为您特别想要 GetCommunitiesResult 节点,所以我将它从信封中剥离出来。由于我遇到了命名空间错误并且命名空间没有为我做任何事情,因此我也将其删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
相关资源
最近更新 更多