从PHP的SOAP响应中获取价值

时间:2018-10-16 11:43:28

标签: php soap soap-client

我从公共WSDL http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL

得到如下响应

如何将sISOCodein转换为php变量?

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <m:CountryCurrencyResponse xmlns:m="http://www.oorsprong.org/websamples.countryinfo">
            <m:CountryCurrencyResult>
                <m:sISOCode>INR</m:sISOCode>
                <m:sName>Rupees</m:sName>
            </m:CountryCurrencyResult>
        </m:CountryCurrencyResponse>
    </soap:Body>
</soap:Envelope>

我写了下面的代码

$wsdl   = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL";
$client = new SoapClient($wsdl, array('trace'=>1));  // The trace param will show you errors stack

// web service input params
$request_param = array("sCountryISOCode" => "IN");
$responce_param = null;
try
{
    $responce_param = $client->CountryCurrency($request_param);
    $xml = simplexml_load_string($responce_param);
    foreach ($xml->xpath('//CountryCurrencyResult') as $item)
    {
        print_r($item);
    }
}
catch (Exception $e) 
{ 
    echo "<h2>Exception Error!</h2>"; 
    echo $e->getMessage(); 
}

如何获取解析响应并将值输入变量?

1 个答案:

答案 0 :(得分:1)

结果是一个stdClass对象。尝试一下。

$wsdl = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL";
$client = new SoapClient($wsdl, array('trace' => 1));  // The trace param will show you errors stack

// web service input params
$request_param = array('sCountryISOCode' => 'IN');
$responce_param = null;
try {
    $responce_param = $client->CountryCurrency($request_param);

    print_r($responce_param->CountryCurrencyResult->sISOCode);
} catch (Exception $e) {
    echo "<h2>Exception Error!</h2>";
    echo $e->getMessage();
}