|
|
|
Accessing Yahoo! Finance from PHPHow to Use PHP to Obtain Stock Quotes from Yahoo! Finance
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:
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 DataCurrent stock quotes can be obtained quite easily from Yahoo! Finance:
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 DataA 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 DataOnce 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>
ConclusionYahoo! 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 ReadingUsing 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.
Comments
Mar 10, 2009 2:51 AM
Guest
:
1 Comment:
|
|
|
|
|
|
|
|