如何使用PHP从这个Web服务获取数据?

时间:2014-06-06 07:02:56

标签: web-services webservice-client

我想从这个网络服务得到回应。

http://www.w3schools.com/webservices/tempconvert.asmx

web_server.php文件

<?php
function celcius($temp){

  ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache

  $wsdl_path = "http://www.w3schools.com/webservices/tempconvert.asmx"; 

  $client = new SoapClient($wsdl_path, array('trace' => 1)); //creating a SOAP Client

  try {
    $result = $client->CelsiusToFahrenheit(array("Celsius" => $temp));
    $sxml =  $client->__getLastResponse();

    $xml = simplexml_load_string($sxml);
    echo $xml;
      $xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');

     $ret_values = array("Fahrenheit"=>string);

     return $ret_values;

  }
  catch (SoapFault $exception) {
    echo $exception;      
  } 
}
?>

temp_websrvc.php文件

<?php
temp_converter();
function temp_converter(){
include 'web_server.php';

$temprature = Array();
echo celcius("27");
$temprature = celcius("27");

     try{

        if($temprature['STATUS'] == 1){
          echo json_encode($temprature);
        }
        else{
          $temprature = array("Fahrenheit" => "");
          echo json_encode($temprature);
          exit();
        }
    }
    catch(Exception $e){
         echo $e->getMessage();
    }
}
?>

1 个答案:

答案 0 :(得分:0)

我暂时没有使用SoapClient,但尝试将摄氏功能更改为:

function celsius($temp) {

    try {
        $result = $client->CelsiusToFahrenheit(array("Celsius" => $temp));
        return $result; 
    }
    catch (SoapFault $exception) {
    echo $exception;      
} 

$farenheitTemperature = 100; //for example
$celsius = celsius($farenheitTemperature);