doulan4939 2015-01-22 15:24
浏览 160
已采纳

PHP - 对JSON的SOAP响应

I am trying to parse SOAP response to JSON. So far I have this code:

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

This code is working and I am getting following 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>

I need to create JSON response from the data which can be found between GetCommunitiesResult tags. How can I do this?

  • 写回答

1条回答 默认 最新

  • dongtongjian3127 2015-01-22 15:33
    关注

    Edited and 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;
    

    Output:

    {"GetCommunitiesResult":{"Communities":[{"CommunityID":"1","CommunityName":" Not Specified"},{"CommunityID":"276","CommunityName":"Bella Toscana"},{"CommunityID":"31","CommunityName":"Crescent Lakes"},{"CommunityID":"62","CommunityName":"Hillcrest Estate"},{"CommunityID":"750","CommunityName":"Sunny Beach"},{"CommunityID":"124","CommunityName":"Terra Verde Resort"},{"CommunityID":"744","CommunityName":"The Dales at West Haven"},{"CommunityID":"158","CommunityName":"Westridge"}]}}

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!