This tutorial will show you a neat way to get stock data from Yahoo Finance.
<?php
Class stock
{
function get_stock_quote($symbol)
{
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1c1" ,$symbol);
$fp = @fopen($url, "r");
if($fp == FALSE)
{
print "Error, Can\'t Open" . "\"$url\"";
}
else
{
$array = @fgetcsv($fp , 4096 , \', \');
@fclose($fp);
$this->name = $array[0];
$this->last = $array[1];
$this->change = $array[2];
}
}
}
$quote = new stock;
$quote->get_stock_quote("XMSR");
$str = $quote->change;
$first = $str{0};
if ($first == "-")
{
$img = "sdown.gif";
}
else if ($first == "+")
{
$img = "sup.gif";
}
else
{
$img = "sblank.gif";
}
print "$quote->name: $quote->last ($quote->change)</strong>" . "<img src=\"images/$img\">";
?>
Class stock
{
This line declares a class with the name of "stock".
function get_stock_quote($symbol)
{
Now we declare a function to easily duplicate this action. 1 parameter is passed into this function "$symbol", which is the companies ticker.
$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1c1" ,$symbol);
$fp = @fopen($url, "r");
The "$url" variable holds the link to Yahoo Finance. we use a sprintf to pass the "$symbol" into the url. After the question mark you will notice a lot of letters. the "s" is the variable on Yahoo\'s side. The "%s" is the variable of "$stock" that we pass in. The "f=" is the start of the values that can be returned from Yahoo. The "s" is the stock symbol, the "ll" is the last, and the "cl" is the change.
if($fp == FALSE)
{
print "Error, Can\'t Open" . "\"$url\"";
}
These couple of lines are simply error checking, which tell us if the connection to the $url failed. The error message will return the $url.
else
{
$array = @fgetcsv($fp , 4096 , \', \');
This continues the script if the $url is opened. We declare an array to store the information returned from the $url. Use fgetcsv because the file that Yahoo produces is a csv (comma seperated value) file. the second parameter is the file length (it must be higher than the last character in the file). The final parameter is the delimiter, we use a [, ].
@fclose($fp); $this->name = $array[0]; $this->last = $array[1]; $this->change = $array[2]; } } }
fclose is used to close the connection to the $url. "$this->" refers to the current class we are in. the next 3 lines are calls to the array filling the three variables found after the "$this->".
$quote = new stock;
$quote->get_stock_quote("XMSR");
$str = $quote->change;
$first = $str{0};
The "$quote" variable declares the "stock" class. The second line is how you pass a ticker into the function. This example uses XMSR (XM Satellite Radio). This part is to display a graphical representation of the stock change. "$str" holds the value of "change". "$first holds the first character of "$str".
if ($first == "-")
{
$img = "sdown.gif";
}
else if ($first == "+")
{
$img = "sup.gif";
}
else
{
$img = "sblank.gif";
}
This checks to see what the character is in the change. Yahoo puts a "+" for up and a "-" for down. If nothing than we place a dash.
print "$quote->name: $quote->last ($quote->change)</strong>" . "<img src=\"images/$img\">";
Finally we print the stock for the world to see. This starts off as a normal print, but then we start using the array variables. Print the name, the last number, and the change. Finally we print the img that displays a graphical version of the stock.
Closing Comments
I hope you find this tutorial useful. A quick idea is to build an array of the tickers so you can diplay many stocks.