gpt4 book ai didi

PHP - 对 JSON 的 SOAP 响应

转载 作者:行者123 更新时间:2023-12-02 21:20:20 24 4
gpt4 key购买 nike

我正在尝试解析对 JSON 的 SOAP 响应。到目前为止我有这段代码:

$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 响应。我怎样才能做到这一点?

最佳答案

已编辑并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":" 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"}]}}

关于PHP - 对 JSON 的 SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com