PHILADELPHIA REFLECTIONS
Musings of a Philadelphia Physician who has served the community for six decades

Return to Home

Related Topics

Using XML to Retrieve Financial Data
It's often desirable to get live financial data and everyone knows. XML is the thing to use but actually writing programs that work takes a bit of trouble. Plus, once you've got the data you need to display it.

Exchange Quotes from XML using PHP (Google Finance)

Google provides an XML feed of delayed quotes. This function returns an array of quotes for an input array of symbols.

<?php
//
// Takes in an array of symbols and returns an array of delayed quotes
// Thanks to http://www.jarloo.com/google-stock-api/
//
/*
On Tuesday December 4, 2012, the following call

    $symbols    = array("MS");
    $quote_list = GoogleStockQuote($symbols);
    print_r($quote_list);

Produced this output:

Array
(
    [MS] => Array
        (
            [pretty_symbol] => MS
            [symbol_lookup_url] => /finance?client=ig&q=MS
            [company] => Morgan Stanley
            [exchange] => NYSE
            [exchange_timezone] => ET
            [exchange_utc_offset] => +05:00
            [exchange_closing] => 960
            [divisor] => 2
            [currency] => USD
            [last] => 16.47
            [high] => 16.61
            [low] => 16.38
            [volume] => 5755086
            [avg_volume] => 19896
            [market_cap] => 32517.05
            [open] => 16.46
            [y_close] => 16.47
            [change] => +0.00
            [perc_change] => 0.00
            [delay] => 0
            [trade_timestamp] => 21 seconds ago
            [trade_date_utc] => 20121204
            [trade_time_utc] => 173319
            [current_date_utc] => 20121204
            [current_time_utc] => 173340
            [symbol_url] => /finance?client=ig&q=MS
            [chart_url] => /finance/chart?q=NYSE:MS&tlf=12
            [disclaimer_url] => /help/stock_disclaimer.html
            [ecn_url] =>
            [isld_last] =>
            [isld_trade_date_utc] =>
            [isld_trade_time_utc] =>
            [brut_last] =>
            [brut_trade_date_utc] =>
            [brut_trade_time_utc] =>
            [daylight_savings] => false
        )
)
*/

function GoogleStockQuote($symbols)
{
  // create the XML query
  $url_part1 = "http://www.google.com/ig/api?";
  $url_part2 = '';
  foreach ($symbols as $symbol)
  {
    $url_part2 .= "stock=" . $symbol . "&";        # stock=VTI&stock=TLT ...
  }
  $url_part2 = substr($url_part2, 0, -1);          # strip off the last ampersand

  $url = $url_part1 . $url_part2;

  // create a SimpleXML object from the XML stream
  $xml = simplexml_load_file($url);

  // pull out the quotes
  foreach ($xml->finance as $quote)
  {
    $symbol = (string)$quote->symbol->attributes()->data;
    foreach ($quote as $info_name => $info)
    {
      if ($info_name == "symbol") continue;
      $quote_list[$symbol][$info_name] = (string)$info->attributes()->data;
    }
  }
  return $quote_list;
}

?>

My thanks to Encode / Decode HTML Entities

(2378)

Please Let Us Know What You Think


(HTML tags provide better formatting)

Because of robot spam we ask you to confirm your comment: we will send you an email containing a link to click. We apologize for this inconvenience but this ensures the quality of the comments. (Your email will not be displayed.)
Thank you.