简体   繁体   中英

Getting Values from a SOAP Response in PHP

I'm getting a response as the below from the public WSDL http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL

How can I get the sISOCodein to a php variable?

<?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>

I have written the code as below

$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(); 
}

How can I get the Parse the response and get the values in to the variables?

The result is a stdClass object. Just try this.

$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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM