从 PHP 中的 SOAP 响应获取值

Getting Values from a SOAP Response in PHP

我从 public 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(); 
}

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

结果是一个 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();
}