Suite101

Accessing Yahoo! Finance from PHP

How to Use PHP to Obtain Stock Quotes from Yahoo! Finance

© Mark Alexander Bain

Oct 10, 2008
Yahoo! Finance and PHP, Mark Alexander Bain
Yahoo! Finance is online and free, and when combined with PHP the end result is a web site that will bring people back again and again.

Every web site developer is looking for those little hooks that will keep people coming back to their pages - one of those hooks could be using PHP to show current stock market information from Yahoo! Finance. Fortunately this is not difficult to set up - all that the web site developer needs to do is:

  • create a library to be used in one or more web pages
  • write a function that will access and process the data from Yahoo! Finance
  • load the library and use the function wherever they need it

However, before setting up that PHP library it's worth looking at the data that's going to be coming from the Yahoo! Finance web site.

Yahoo! Finance Data

Current stock quotes can be obtained quite easily from Yahoo! Finance:

  • the stock quotes are obtained via the url - http://quote.yahoo.com/d/quotes.csv
  • the url needs some additional pieces information, and these are:
    • s - a list of companies or markets for which stock quotes are required. Yahoo! Finance symbols are entered rather than the full name, for example:
      • ^IXIC = NASDAQ Composite Average
      • ^NIN = NYSE International 100
      • ^FTSE = FTSE 100
      • RHT = Red Hat Inc.
    • f - a list of fields to be output, such as:
      • n - the company name
      • l1 - the latest stock quote
      • c1 - the change in value of the stock price
    • e=.csv - the output type will be in csv (comma separated variable format)

So, for example to examine the current state of some of the stock markets the full web url would be:

http://quote.yahoo.com/d/quotes.csv?s=^IXIC,^NIN&f=nl1c1&e=.csv

Obviously the next step is to write a PHP function that will read the information that this url supplies as a data stream.

A PHP Function to Process Yahoo! Finance Data

A PHP library is created by saving the function into a text file - in this example the library is going to be called yahoo_finance.php and this will contain a single function:

<?php
function show_stock_quotes ($stock_list) {

The function will, by default, return the details of some of the stock markets:

if (! $stock_list) {
$stock_list = "^IXIC,^DJA,^NIN,^FTSE";
}

The function will then use either an input list or the default list to create the correct url:

$url = "http://quote.yahoo.com/d/quotes.csv?s="
. $stock_list . "&f=nl1c1&e=.csv";

and then read in the data stream from the Yahoo! Finance web site:

$filesize = 2000;
$handle = fopen($url, "r");
$raw_quote_data = fread($handle, $filesize);
fclose($handle);

The data will be in the format:

"Novell",4.37,0.47
"Microsoft",23.23,-1.68

and so the next stage is to load the data into an array (using the new line as a delimiter):

$quote_array = explode("\n", $raw_quote_data);

finally the results can be output (taking care to remove the additional quotation marks):

echo "<table>";
echo "<th>Name</th> <th>Stock Quote</th> <th>Change";
foreach ($quote_array as $quote_value) {
echo "<tr>";
$quote = explode(",", $quote_value);
$symbol = str_replace("\"", "", $quote[0]);
$value = $quote[1];
$change = $quote[2];
echo "<td>$symbol</td> <td>$value</td> <td>$change</td>";
echo "</tr>";
}
echo "</table>";
}
?>

Using the PHP Function to Display Yahoo! Finance Data

Once the function and library have been saved then they can be used in a PHP web page (for instance index.php) to display the default stock market data:

<?php
include ("yahoo_finance.php");
show_stock_quotes (False);
?>

or used in a PHP file that can process the input from a textarea box:

<?php
include ("yahoo_finance.php");
$symbol_list = str_replace("\n", ",", $_REQUEST["symbol_list"]);
$symbol_list = str_replace("\r", ",", $symbol_list);
show_stock_quotes (False);
show_stock_quotes ($symbol_list);
?>

If this code is stored in a file (for example yahoo_finance_lookup.php) then it can be called from a form:

<form method=post action=yahoo_finance_lookup.php>
<textarea name=symbol_list></textarea>
<input type=submit>
</form>

Conclusion

Yahoo! Finance is a very useful resource for both analysts and programmers alike, but if it also make a web site more attractive then it must be worth considering - especially when it's so easy to incorporate into PHP.

Additional Reading

Using Yahoo! Financial Stock Quotes


The copyright of the article Accessing Yahoo! Finance from PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish Accessing Yahoo! Finance from PHP in print or online must be granted by the author in writing.


Yahoo! Finance and PHP, Mark Alexander Bain
Yahoo! Finance Stock Prices on in a New Web Page, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Mar 10, 2009 2:51 AM
Guest :
Nice article .....
1 Comment: