I am ignorant when it comes to SOAP. I am performing a web service call:
<?php
// define the SOAP client using the url for the service
$client = new soapclient('http://www.xignite.com/xMetals.asmx?WSDL', array('trace' => 1));
// create an array of parameters
$param = array(
'Type' => "XAU",
'Currency' => "USD");
// call the service, passing the parameters and the name of the operation
$result = $client->GetLastRealTimeMetalQuote($param);
// assess the results
if (is_soap_fault($result)) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
?>
and when I run the script i get:
Result
stdClass Object
(
[GetLastRealTimeMetalQuoteResult] => stdClass Object
(
[Outcome] => Success
[Identity] => IP
[Delay] => 0.00开发者_StackOverflow6
[Symbol] => XAUUSDO
[Type] => XAU
[Currency] => USD
[Date] => 8/1/2011
[Time] => 11:18:48 PM
[Rate] => 1618.88500977
[Bid] => 1618.55004883
[BidTime] => 11:18:48 PM
[Ask] => 1619.2199707
[AskTime] => 11:18:48 PM
)
)
How do I separate the [Bid] out from the rest of the result and store it in a variable.
Or better yet how can I pull out the array?
Don't let the stdClass object mix you up - this is simply an array that is represented with object notation. So, $result['GetLastRealTimeMetalQuoteResult']['Bid']
(a normal associative array) becomes $result->GetLastRealTimeMetalQuoteResult->Bid
- same values, just a different notation.
You get stdClass objects when a value is typecast into an object, which the SOAP library does. See: http://php.net/manual/en/reserved.classes.php For some more detail about stdClass, check out this article: http://krisjordan.com/dynamic-properties-in-php-with-stdclass
If you'd like to convert the stdClass to an array, unfortunately you'll have to use a little function:
function objToArray($obj=false) {
if (is_object($obj))
$obj= get_object_vars($obj);
if (is_array($obj)) {
return array_map(__FUNCTION__, $obj);
} else {
return $obj;
}
}
$somevar = $result->GetLastRealTimeMetalQuoteResult->Bid;
精彩评论