Yesterday February 23rd, 2010 I released version 1.1 of my YahooFinanceAPI PHP library. This release adds in support for localization of the information so that you can get the stock quote time and date information in your local timezone, if Yahoo Finance supports your local.

To set your local simply set the local property of YahooFinanceAPI to a valid location.

$api->local = YahooFinance_Location::FRANCE;

To make it easier on users I have added the YahooFinance_Location class that contains constants of all the valid locals supported by Yahoo Finance. To see full sample code as well as staying up-to-date on changes to the library then please visit the PHP Wrapper Library for Yahoo Finance Stock Quotes API page that is maintained with the latest version for download and usage examples.

, , , , ,

This short tutorial shows how to access Stock Information from Yahoo Finance through an AJAX call utilizing JSONP. For the tutorial the following information is required.

  • JQuery - Javascript library with built in AJAX and JSONP functions
  • YahooFinanceAPI - PHP library that utilizes Yahoo Finance to return stock information from an underlying call to download.finance.yahoo.com/d/qutoes.csv – Please note that you can utilize this library to create your own web page that will return various stock information. For the purpose of the example we will be accessing a url on my server to showcase the cross domain capability.

Step 1: Create a sample html page that includes the JQuery library

<!DOCTYPE HTML PUBLIC
   "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <script src="http://code.jquery.com/jquery-latest.js"/>
  </head>
  <body>
    <div id="quote">
 
    </div>
  </body>
</html>

Step 2: Create the table that will hold the stock information returned by the AJAX call

<table cellspacing="0" cellpadding="3" border="1">
  <tr>
    <th>Symbol</th>
    <th>Last Trade</th>
    <th>Last Trade Time</th>
    <th>Change</th>
    <th>Open</th>
    <th>Previous Close</th>
    <th>Day's Low</th>
    <th>Day's High</th>
    <th>Volume</th>
  </tr>
  <tr>
    <td id="symbol"></td>
    <td id="lastTrade"></td>
    <td id="lastTradeTime"></td>
    <td id="change"></td>
    <td id="open"></td>
    <td id="previousClose"></td>
    <td id="daysLow"></td>
    <td id="daysHigh"></td>
    <td id="volume"></td>
  </tr>
</table>

Step 3: Define the JQuery JavaScript code that will call make an AJAX call for the specified stock information

  1. Create the document ready function so the JavaScript is not called until the page is loaded.
    $(document).ready(function(){
    });
  2. perform the AJAX call for JSONP data using the getJSON function of JQuery
    $.getJSON(
      "http://mdbitz.com/testing/PHPYahooFinance/finance.php?symbol=DELL&amp;callback=?",
      function( data ) {
      }
    );
  3. Output the returned data to the appropriate cells of the Table.
    $("#symbol").text( data.symbol );
    $("#previousClose" ).text( data.previousClose );
    $("#open" ).text( data.open );
    $("#lastTrade").text( data.lastTrade );
    $("#lastTradeTime").text( data.lastTradeTime );
    $("#change").text( data.change );
    $("#daysLow").text( data.daysLow );
    $("#daysHigh").text( data.daysHigh );
    $("#volume").text( data.volume);

Step 4: View your sample page

Symbol Last Trade Last Trade Time Change Open Previous Close Day’s Low Day’s High Volume
DELL 16.13 10.19AM +0.31 15.77 15.82 15.75 16.20 7162443

Full Sample HTML Source

<!DOCTYPE HTML PUBLIC
  "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
      $(document).ready(function(){
        $.getJSON("http://mdbitz.com/testing/PHPYahooFinance/finance.php?symbol=DELL&callback=?",
          function(data){
            $("#symbol").text( data.symbol );
            $("#previousClose" ).text( data.previousClose );
            $("#open" ).text( data.open );
            $("#lastTrade").text( data.lastTrade );
            $("#lastTradeTime").text( data.lastTradeTime );
            $("#change").text( data.change );
            $("#daysLow").text( data.daysLow );
            $("#daysHigh").text( data.daysHigh );
            $("#volume").text( data.volume);
          }
        );
     });
     </script>
  </head>
  <body>
    <div id="quote">
      <table cellspacing="0" cellpadding="3" border="1">
        <tr>
          <th>Symbol</th>
          <th>Last Trade</th>
          <th>Last Trade Time</th>
          <th>Change</th>
          <th>Open</th>
          <th>Previous Close</th>
          <th>Day's Low</th>
          <th>Day's High</th>
          <th>Volume</th>
        </tr>
        <tr>
          <td id="symbol"></td>
          <td id="lastTrade"></td>
          <td id="lastTradeTime"></td>
          <td id="change"></td>
          <td id="open"></td>
          <td id="previousClose"></td>
          <td id="daysLow"></td>
          <td id="daysHigh"></td>
          <td id="volume"></td>
        </tr>
      </table>
    </div>
  </body>
</html>
, , , ,

YahooFinanceAPI is an open source php library for interacting with the Yahoo Finance Stock API. For those of you unfamiliar with Yahoo’s Stock API please review my previous post Yahoo Finance Stock API. This library contains an easy interface for obtaining information on one or more stocks. For a quick demo of the library feel free to go to http://mdbitz.com/testing/PHPYahooFinance/finance.php?symbol=DELL which will return JSON coded response of some of the basic information that can be obtained via the library. Also feel free to swap out the symbol in the url with any of your choice, if it is an invalid symbol you will simply not get any information.

How do I use the YahooFinanceAPI library?

The YahooFinanceAPI was constructed so that users need to simply follow a basic 6 set process to get any stock information they are looking for.

Step 1: Obtain the main YahooFinanceAPI class

Before you are able to utilize the YahooFinanceApi library you will need to require or include it into your php script.

require_once(dirname(__FILE__) . '/YahooFinanceAPI.php');

Step 2: Register the autoloader

To mitigate changes in potential future versions this library utilizes an autoloader to load its classes when and if needed. To properly function you will need to register its autoloader by the following command.

spl_autoload_register(array('YahooFinanceAPI', 'autoload'));<pre></p>
<p style="padding-left: 30px;"><strong>Step 3</strong>: Identify the information to be returned</p>
<p style="padding-left: 60px;">Now that the library is properly configured in your script lets define the information that is to be returned for each of the symbols or stocks being looked up. This is done by using the <em>addOption</em> function of the YahooFinanceAPI instance. For a complete list of options please view the YahooFinance_Options class.
<pre lang="php" escaped="true">$api = new YahooFinanceAPI();
// set options
$api-&gt;addOption("symbol");
$api-&gt;addOption("previousClose");
$api-&gt;addOption("open");
$api-&gt;addOption("lastTrade");
$api-&gt;addOption("lastTradeTime");
$api-&gt;addOption("change" );
$api-&gt;addOption("daysLow" );
$api-&gt;addOption("daysHigh" );
$api-&gt;addOption("volume" );

Step 4: Identity the symbols (stocks) to obtain the information for

After defining the information to be returned we define they symbols (stocks) to be returned. This is done by the addSymbol function. To find symbols you can search Yahoo Finance for the company you are interested in.

$api->addSymbol("DELL" );

Step 5: Query for the Information

At this point we have configured the library and defined the stocks and information that we are looking for and are ready to perform our query of the Yahoo Finance Stock API.

$result = $api->getQuotes();

Step 6: Use the Stock Information

The result object returned contains a code property to tell you if the query was successfull 2XX or failed 4XX. In addition the data property contains an array of the requested information for each of the symbols.

$results->code //-- returns the http response code
$results->data //-- array of Quote Objects
$results->data[0] ->symbol //-- returns the symbol name of the first Quote Object

Download the YahooFinanceAPI Library

The YahooFinanceAPI is copyright MDBitz – Matthew John Denton under the GNU Public License. The current release is available for download at http://mdbitz.com/downloads/YahooFinanceAPI.zip. The archive contains the php classes as well as a test.php file that demonstrates how to use the library.

Feedback Appreciated

Please leave your comments, suggestions, and/or questions below. And if you need any additional support please feel free to send me an email at info@mdbitz.com.

, , , , ,