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.

Updates in Release 1.0

  • isSuccess method added to YahooFinance_Result object to check if request was successful.
  • Bug fixes for typo errors in YahooFinance_Options
  • toXML method added to YahooFinance_Result and YahooFinance_Quote
  • STRICT and LOOSE mode added to YahooFinanceAPI to throw or supress exceptions

Online Documentation

The YahooFinanceAPI PHP Library is fully documented using phpDocumentor. The online documentation contains a full code example as well as class and method descriiptions and parameters.

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

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

Step 2: Register the autoloader

spl_autoload_register(array('YahooFinanceAPI', 'autoload'));

Step 3: Identify the information to be returned

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 addOption function of the YahooFinanceAPI instance. For a complete list of options please view the YahooFinance_Options class.

$api = new YahooFinanceAPI();
 
// set options
$api->addOption("symbol");
$api->addOption("previousClose");
$api->addOption("open");
$api->addOption("lastTrade");
$api->addOption("lastTradeTime");
$api->addOption("change" );
$api->addOption("daysLow" );
$api->addOption("daysHigh" );
$api->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

$result = $api->getQuotes();

Step 6: Use the Stock Information

if( $result->isSuccess() ) {
    $quotes = $result->data;
    foreach( $quotes as $quote ) {
        $symbol = $quote->symbol;
        $lastTrade = $quote->lastTrade;
    }
}

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-1.0.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.

, , , , ,

JavaScript Object Notation or JSON for short is a data interchange format commonly used in AJAX Web Applications. The format is human readable and can represent simple data structures and Objects by use of associative arrays.

Syntax Formatting

The JSON format is lightweight in that the properties of the JSON Data Object are encoded by  curly braces. And the properties of the object are represented by key-value pairs delimited by the colon character and separated by a comma. In addition to an array value is defined by square brackets that can contain objects or values. Using these basic building blocks you are able to represent complex data.

Basic Examples

Example 1: Stock Information

{
    "symbol":"DELL",
    "open":"14.23",
    "lastTrade":"13:85"
}

Example 2: A Book Object with title, isbn, and author(s)

{
    "title": "First Lord's Fury",
    "isbn": "102304312",
    "authors": [
                 { "fullName": "Jim Butcher", 
                   "lastName":"Butcher", 
                   "firstName":"Jim" 
                 }
               ]
}

JSONP for cross Domain JSON Data

JavaScript Object Notation with Padding (JSONP) is a method that a server can implement for allowing other sites to access and utilized data from your server. Do to browser security it is not possible to access JSON data directly from a remove server. JSONP resolves this issue by allowing the caller to pass in a callback function that the JSON data gets wrapped in so that the data is executed by your defined function. The really nice element of this is that JQUERY and other JS libraries already contain AJAX support for you to easily create these calls.

JSONP server response format

For JSONP to function the server must except a parameter to be used as the callback function in addition to any data it needs to obtain the requested information.

http://www.example.com/?callback=XYZ

What the server needs to do is take the callback parameter and wrap the json data in a call to the callback function as:

XYZ( JSON-DATA )

That is all there is to it.

, , , ,

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.

, , , , ,