首页 > 解决方案 > 如何使用 php 将 xml soap api 结果打印为 json 或 xml

问题描述

**如何使用 php 将 xml soap api 结果打印为 json 或 xml ** 我以前没有使用过 SOAP API。我想使用 XML 数据请求执行 SOAP API。我试过但没有得到结果。

  // The URL to POST to
  $url = "https://api.tbotechnology.in/HotelAPI_V7/HotelService.svc";
  // The value for the SOAPAction: header
  $action = "http://TekTravel/HotelBookingApi/CountryList";

  // Get the SOAP data into a string, I am using HEREDOC syntax
  // but how you do this is irrelevant, the point is just get the
  // body of the request into a string
  $mySOAP = <<<EOD
<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:hot="http://TekTravel/HotelBookingApi">
  <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <hot:Credentials UserName="***" Password="***"> </hot:Credentials> 
    <wsa:Action>http://TekTravel/HotelBookingApi/CountryList</wsa:Action>
    <wsa:To>https://api.tbotechnology.in/HotelAPI_V7/HotelService.svc</wsa:To>
  </soap:Header>
  <soap:Body>
    <hot:CountryListRequest/>
  </soap:Body>
</soap:Envelope>
EOD;

$headers = array(
'Content-Type: application/soap+xml; charset=UTF-8',
'Content-Length: '.strlen($mySOAP),
'SOAPAction: ' .$action
);

// Build the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
die('cURL error: '.curl_error($ch)."<br />\n");
} else {
echo "Success!<br />\n";
echo $result;
}
curl_close($ch);

标签: phpxmlapisoap

解决方案


推荐阅读