0

试图从谷歌股票 api 中提取一些数据。所以只是试图提取它。

$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>';


?>

这给了我下面的输出。但我似乎无法获得任何价值。我迷失在数据中,不知道从哪一端开始。

我假设$xml-> finance[0]->symbol会给我“ FUM1V ”,而$xml-> Finance[1]->symbol会给我KNEBV但无论我尝试什么组合,我都会得到空白。好的,这可能是一个不好的例子,但在翻译中丢失了。

关于如何“循环”并打印每个符号和一些值的任何想法?

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
                                )

                        )

                )

        )

)
4

2 回答 2

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


}
于 2013-06-17T22:06:40.353 回答
0

您需要的数据存储在该 XML 的节点属性中,因此这里有一个简单的 PHP 代码片段,可以帮助您上路。

$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 />';
}

输出:

Fortum Oyj
FUM1V

KONE Corporation
KNEBV
于 2013-06-17T22:48:44.810 回答