Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Stock Quotes

Stock Quotes

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.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Stock Quotes

 saul  Mon Sep 17, 2007 12:26 pm  

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /var/www/test/stock/bb.php on line 14

Parse error: syntax error, unexpected $end in /var/www/test/stock/bb.php on line 42
 K  Sat Sep 27, 2008 2:03 am  
else
{
$array = @fgetcsv($fp , 4096 , ', ');
Should be:
else
{
$array = @fgetcsv($fp , 4096 , ",");
 Guest  Sat Sep 27, 2008 2:05 am  
NOT AS in your tutorial:
else
{
$array = @fgetcsv($fp , 4096 , \', \');
 Commodity Tips  Tue Dec 13, 2011 11:42 pm  
Thanks for all. I have understand your stuff previous to and you are just too magnificent.I actually like what you have acquired here, certainly like what you are stating and the way in which you say it. You make it entertaining and you still care for to keep it
sensible. I can not wait to read far more from you. This is actually a tremendous site.
Regards: Commodity Tips Very efficiently written post. It will be valuable to everyone who uses it, as well as myself. Keep doing what you are doing looking forward to more posts.
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles