Anto S
Anto S

Reputation: 2449

How to get node value from the API xml

I am trying to get curreny exchange rate from an API. Code as below

<?php

$return = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22AEDUSD%22%29&env=store://datatables.org/alltableswithkeys");

$xml=simplexml_load_string($return) or die("Error: Cannot create object");

echo '<pre>'; print_r($xml);

?>

Above Code provides the XML

SimpleXMLElement Object
(
    [results] => SimpleXMLElement Object
        (
            [rate] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => AEDUSD
                        )

                    [Name] => AED/USD
                    [Rate] => 0.2722
                    [Date] => 7/14/2015
                    [Time] => 7:17am
                    [Ask] => 0.2723
                    [Bid] => 0.2722
                )

        )

)

I would like to get Rate node value [Rate] => 0.2722 I tried below

<?php
    echo '<pre>'; print_r($xml->results->rate->Rate);
?>

It gives the result as below,

SimpleXMLElement Object
(
    [0] => 0.2722
)

How to get rid of the Object and take the value?

Upvotes: 0

Views: 53

Answers (1)

Daniel Krom
Daniel Krom

Reputation: 10058

try read this question how to get value of simple xml object

cast it to string

echo '<pre>'; print_r((string)$xml->results->rate->Rate);

Upvotes: 1

Related Questions