如何从PHP获取SOAP响应

时间:2017-07-26 10:57:34

标签: php xml soap

我正在开发一个SOAP Web服务

我调用了Web服务以获取下面代码的所有方法

$client = new SoapClient("https://www.xxxxxxx.com/eSalesTest/webservice/xxx.asmx?wsdl");
var_dump($client->__getFunctions()); 
//var_dump($client->__getTypes());


which gave me something like

 1 => string 'Save3rdPartyPolicyResponse Save3rdPartyPolicy(Save3rdPartyPolicy $parameters)' (length=77)
  2 => string 'SaveTravelPolicyResponse SaveTravelPolicy(SaveTravelPolicy $parameters)' (length=71)
  3 => string 'Book3rdPartyMotorResponse Book3rdPartyMotor(Book3rdPartyMotor $parameters)' (length=74)
  4 => string 'BookComprehensiveMotorResponse BookComprehensiveMotor(BookComprehensiveMotor $parameters)' (length=89)
  5 => string 'BookTravelPolicyResponse BookTravelPolicy(BookTravelPolicy $parameters)' (length=71)
  6 => string 'GetVehicleMakesResponse GetVehicleMakes(GetVehicleMakes $parameters)' (length=68)
  7 => string 'GetThirdPartyCategoriesResponse GetThirdPartyCategories(GetThirdPartyCategories $parameters)' (length=92)
  8 => string 'GetThridPartyPremiumResponse GetThridPartyPremium(GetThridPartyPremium $parameters)' (length=83)
  9 => string 'SaveComprehensiveMotorResponse SaveComprehensiveMotor(SaveComprehensiveMotor $parameters)' (length=89)
  10 => string 'BookMIPResponse BookMIP(BookMIP $parameters)' (length=44)
  11 => string 'SaveMIPResponse SaveMIP(SaveMIP $parameters)' (length=44)

但是,我想要的是能够获得响应请求并将其填充到我的表单上。

我已经使用soapUI做了那个,并以xml格式获得了响应,如

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetStatesResponse xmlns="http://tempuri.org/">
         <GetStatesResult>[{"Code":"6","Text":"Abia"},{"Code":"7","Text":"Adamawa"},{"Code":"8","Text":"Akwa Ibom"},{"Code":"9","Text":"Anambra"},{"Code":"10","Text":"Bauchi"},{"Code":"11","Text":"Bayelsa"},{"Code":"12","Text":"Benue"},{"Code":"13","Text":"Bornu"},{"Code":"14","Text":"Cross River "},{"Code":"15","Text":"Delta"},{"Code":"16","Text":"Ebonyi "},{"Code":"17","Text":"Edo"},{"Code":"18","Text":"Ekiti"},{"Code":"19","Text":"Enugu"},{"Code":"5","Text":"Federal Capital Territory"},{"Code":"42","Text":"Foreign Offices"},{"Code":"20","Text":"Gombe"},{"Code":"21","Text":"Imo"},{"Code":"22","Text":"Jigawa"},{"Code":"23","Text":"Kaduna"},{"Code":"24","Text":"Kano"},{"Code":"25","Text":"Katsina"},{"Code":"26","Text":"Kebbi"},{"Code":"27","Text":"Kogi"},{"Code":"28","Text":"Kwara"},{"Code":"29","Text":"Lagos"},{"Code":"30","Text":"Nasarawa"},{"Code":"31","Text":"Niger"},{"Code":"32","Text":"Ogun"},{"Code":"33","Text":"Ondo"},{"Code":"34","Text":"Osun"},{"Code":"35","Text":"Oyo"},{"Code":"36","Text":"Plateau"},{"Code":"37","Text":"Rivers"},{"Code":"38","Text":"Sokoto"},{"Code":"39","Text":"Taraba"},{"Code":"40","Text":"Yobe"},{"Code":"41","Text":"Zamfara"}]</GetStatesResult>
      </GetStatesResponse>
   </soap:Body>
</soap:Envelope>

请更好地解释我正在做的事情或下一步要做的事情

1 个答案:

答案 0 :(得分:0)

首先尝试这个例子:

$client = new SoapClient('http://www.webservicex.net/geoipservice.asmx?WSDL');

$parameters = array(
    'IPAddress' => '8.8.8.8',
);
$result = $client->GetGeoIP($parameters);

print_r($result);

然后试试这个:

// Change the url
$client = new SoapClient('https://www.xxxxxxx.com/eSalesTest/webservice/xxx.asmx?wsdl');

$parameters = array(
    'token' => 'my-secret-token'
);
$result = $client->GetStates($parameters);

// Decode the json string
$json = $result->GetStatesResult;
$items = json_decode($json, true);

// Output
echo '<select name="states">';
foreach ($items as $item) {
    $code = htmlspecialchars($item['Code'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    $text = htmlspecialchars($item['Text'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    echo sprintf('<option value="%s">%s</option>', $code, $text);
}
echo '</select> ';