0

Trying to extract some data from google stock api. So just trying to extract it.

$t=time();
echo(date("H:i",$t))."<br><br>";

$xml="http://www.google.com/ig/api?stock=KNEBV&stock=FUM1V"; 

$xml = json_decode(json_encode((array) simplexml_load_file($xml)), 1);
echo '<pre>';
print_r($xml);
echo '</pre>';


?>

That gives me the output below. But I just don't seems to be able to grab any value. I am so lost in the data I don't know which end to start.

I would assume $xml-> finance[0]->symbol would give me "FUM1V" and $xml-> finance[1]->symbol would give me KNEBV but I keep getting blanks whatever combo I try. Ok, it might be a bad example but lost in translation.

Any ideas on how to get "loop" through and print each symbol and some values?

Array
(
    [@attributes] => Array
        (
            [version] => 1
        )

    [finance] => Array
        (
            [0] => Array
                (
                    [@attributes] => Array
                        (
                            [module_id] => 0
                            [tab_id] => 0
                            [mobile_row] => 0
                            [mobile_zipped] => 1
                            [row] => 0
                            [section] => 0
                        )

                    [symbol] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => FUM1V
                                )

                        )

                    [pretty_symbol] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => FUM1V
                                )

                        )

                    [symbol_lookup_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /finance?client=ig&q=FUM1V
                                )

                        )

                    [company] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => Fortum Oyj
                                )

                        )

                    [exchange] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => HEL
                                )

                        )

                    [exchange_timezone] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [exchange_utc_offset] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [exchange_closing] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [divisor] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 2
                                )

                        )

                    [currency] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => EUR
                                )

                        )

                    [last] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 14.68
                                )

                        )

                    [high] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 14.77
                                )

                        )

                    [low] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 14.59
                                )

                        )

                    [volume] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 1216170
                                )

                        )

                    [avg_volume] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 1607
                                )

                        )

                    [market_cap] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 13041.22
                                )

                        )

                    [open] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 14.60
                                )

                        )

                    [y_close] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 14.59
                                )

                        )

                    [change] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => +0.09
                                )

                        )

                    [perc_change] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 0.62
                                )

                        )

                    [delay] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 0
                                )

                        )

                    [trade_timestamp] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 5 hours ago
                                )

                        )

                    [trade_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 20130617
                                )

                        )

                    [trade_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 152953
                                )

                        )

                    [current_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 20130617
                                )

                        )

                    [current_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 212211
                                )

                        )

                    [symbol_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /finance?client=ig&q=FUM1V
                                )

                        )

                    [chart_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /finance/chart?q=HEL:FUM1V&tlf=12
                                )

                        )

                    [disclaimer_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /help/stock_disclaimer.html
                                )

                        )

                    [ecn_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [isld_last] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [isld_trade_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [isld_trade_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [brut_last] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [brut_trade_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [brut_trade_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [daylight_savings] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => true
                                )

                        )

                )

            [1] => Array
                (
                    [@attributes] => Array
                        (
                            [module_id] => 0
                            [tab_id] => 0
                            [mobile_row] => 0
                            [mobile_zipped] => 1
                            [row] => 0
                            [section] => 0
                        )

                    [symbol] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => KNEBV
                                )

                        )

                    [pretty_symbol] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => KNEBV
                                )

                        )

                    [symbol_lookup_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /finance?client=ig&q=KNEBV
                                )

                        )

                    [company] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => KONE Corporation
                                )

                        )

                    [exchange] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => HEL
                                )

                        )

                    [exchange_timezone] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [exchange_utc_offset] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [exchange_closing] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [divisor] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 2
                                )

                        )

                    [currency] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => EUR
                                )

                        )

                    [last] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 65.95
                                )

                        )

                    [high] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 67.00
                                )

                        )

                    [low] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 65.95
                                )

                        )

                    [volume] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 249502
                                )

                        )

                    [avg_volume] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 303
                                )

                        )

                    [market_cap] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 16911.31
                                )

                        )

                    [open] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 66.10
                                )

                        )

                    [y_close] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 66.05
                                )

                        )

                    [change] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => -0.10
                                )

                        )

                    [perc_change] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => -0.15
                                )

                        )

                    [delay] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 0
                                )

                        )

                    [trade_timestamp] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 5 hours ago
                                )

                        )

                    [trade_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 20130617
                                )

                        )

                    [trade_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 152952
                                )

                        )

                    [current_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 20130617
                                )

                        )

                    [current_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 212211
                                )

                        )

                    [symbol_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /finance?client=ig&q=KNEBV
                                )

                        )

                    [chart_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /finance/chart?q=HEL:KNEBV&tlf=12
                                )

                        )

                    [disclaimer_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => /help/stock_disclaimer.html
                                )

                        )

                    [ecn_url] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [isld_last] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [isld_trade_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [isld_trade_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [brut_last] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [brut_trade_date_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [brut_trade_time_utc] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => 
                                )

                        )

                    [daylight_savings] => Array
                        (
                            [@attributes] => Array
                                (
                                    [data] => true
                                )

                        )

                )

        )

)
hakre
  • 193,403
  • 52
  • 435
  • 836
Tony L
  • 41
  • 4
  • Even you might not imagine it, but it is more easy to learn how simplexml works then converting into an array via json_encode/json_decode. SimpleXML has been created exactly for such tasks. – hakre Jun 18 '13 at 07:39

2 Answers2

0
$t=time();
echo(date("H:i",$t))."<br><br>";

$xml="http://www.google.com/ig/api?stock=KNEBV&stock=FUM1V"; 


$xml=simplexml_load_file($xml);


foreach($xml->finance as $finance) {

    echo $finance->symbol['data'].'<br>';

    echo $finance->company['data'].'<br>';

    //etc.etc////


}
sinisake
  • 11,240
  • 2
  • 19
  • 27
0

The data you're after is stored in the node attributes in that XML, so here's a simple PHP code snippet to get you on your way.

$xml = <<<XML
<?xml version="1.0"?><xml_api_reply version="1"><finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><symbol data="FUM1V"/><pretty_symbol data="FUM1V"/><symbol_lookup_url data="/finance?client=ig&amp;q=FUM1V"/><company data="Fortum Oyj"/><exchange data="HEL"/><exchange_timezone data=""/><exchange_utc_offset data=""/><exchange_closing data=""/><divisor data="2"/><currency data="EUR"/><last data="14.68"/><high data="14.77"/><low data="14.59"/><volume data="1216170"/><avg_volume data="1607"/><market_cap data="13041.22"/><open data="14.60"/><y_close data="14.59"/><change data="+0.09"/><perc_change data="0.62"/><delay data="0"/><trade_timestamp data="7 hours ago"/><trade_date_utc data="20130617"/><trade_time_utc data="152953"/><current_date_utc data="20130617"/><current_time_utc data="223403"/><symbol_url data="/finance?client=ig&amp;q=FUM1V"/><chart_url data="/finance/chart?q=HEL:FUM1V&amp;tlf=12"/><disclaimer_url data="/help/stock_disclaimer.html"/><ecn_url data=""/><isld_last data=""/><isld_trade_date_utc data=""/><isld_trade_time_utc data=""/><brut_last data=""/><brut_trade_date_utc data=""/><brut_trade_time_utc data=""/><daylight_savings data="true"/></finance><finance module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><symbol data="KNEBV"/><pretty_symbol data="KNEBV"/><symbol_lookup_url data="/finance?client=ig&amp;q=KNEBV"/><company data="KONE Corporation"/><exchange data="HEL"/><exchange_timezone data=""/><exchange_utc_offset data=""/><exchange_closing data=""/><divisor data="2"/><currency data="EUR"/><last data="65.95"/><high data="67.00"/><low data="65.95"/><volume data="249502"/><avg_volume data="303"/><market_cap data="16911.31"/><open data="66.10"/><y_close data="66.05"/><change data="-0.10"/><perc_change data="-0.15"/><delay data="0"/><trade_timestamp data="7 hours ago"/><trade_date_utc data="20130617"/><trade_time_utc data="152952"/><current_date_utc data="20130617"/><current_time_utc data="223403"/><symbol_url data="/finance?client=ig&amp;q=KNEBV"/><chart_url data="/finance/chart?q=HEL:KNEBV&amp;tlf=12"/><disclaimer_url data="/help/stock_disclaimer.html"/><ecn_url data=""/><isld_last data=""/><isld_trade_date_utc data=""/><isld_trade_time_utc data=""/><brut_last data=""/><brut_trade_date_utc data=""/><brut_trade_time_utc data=""/><daylight_savings data="true"/></finance></xml_api_reply>
XML;

$sxe = new SimpleXMLElement($xml);

// In your case uncomment the following 2 lines
// $url = 'http://www.google.com/ig/api?stock=KNEBV&stock=FUM1V';
// $sxe = new SimpleXMLElement($url, 0, true);

foreach ($sxe->finance as $finance) {
    echo $finance->company->attributes()->data, '<br />';
    echo $finance->symbol->attributes()->data, '<br /><br />';
}

Output:

Fortum Oyj
FUM1V

KONE Corporation
KNEBV
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43