PHP - 对 JSON 的 SOAP 响应
PHP - SOAP response to JSON
我正在尝试解析对 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"}]}}
我正在尝试解析对 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"}]}}