Yahoo Stock Market retrieval system

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Yahoo Stock Market retrieval system

Post by phice »

Small class-based php program to grab the latest stocks (specified in the url [stock.php?symbols=YHOO,MSFT,etc...]). I forget where I got the original source from, but I edited it to enhance a small section of my website. Enjoy :)

Code: Select all

<?php
// Code by:   Don Wilson
// Edited:     October 24, 2003
class stock
{
	function getLastTrade($symbol)
	{
		$file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");
			$read = fread($file, 2000);
			fclose($file);
		$read = str_replace(""", "", $read);
		$read = explode(",", $read);
		return $read[1];
	}

	function getChange($symbol)
	{
		$file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");
			$read = fread($file, 2000);
			fclose($file);
		$read = str_replace(""", "", $read);
		$read = explode(",", $read);
		return $read[4];
	}

	function getDateClosed($symbol)
	{
		$file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");
			$read = fread($file, 2000);
			fclose($file);
		$read = str_replace(""", "", $read);
		$read = explode(",", $read);
		return $read[2] . " " . $read[3];
	}

}

$stock = new Stock();

echo "<pre>\n";
if(isset($symbols))
{
	$symbols = explode(",", $symbols);
	foreach($symbols as $symbol)
	{
	$change = $stock->getChange($symbol);
	if($change < 0)
	  $change = "<font color=red>{$change}</font>";
	elseif($change > 0)
	  $change = "<font color=green>{$change}</font>";
	else
	  $change = "0.00";
	  
		echo "Symbol:			" . $symbol										. "\n";
		echo "Last Trade:		" . $stock->getLastTrade($symbol)		. "\n";
		echo "Change:			" . $change			. "\n";
		echo "Date Closed:		" . $stock->getDateClosed($symbol)	. "\n";
		echo "\n";
	}
}
echo "</pre>\n";
?>
Image Image
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Sounds silly... but where could i get all the stock exchange symbols?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Phice, do you know how legal it would be to use these stocks/prices in a game that makes profit?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can use them as entropy data for seeding randoms pretty easy.. however using them straight.. I dunno.. need to talk to a lawyer, but I'd guess it's "okay"
computingfuture
Forum Newbie
Posts: 6
Joined: Thu Jun 24, 2004 12:49 pm
Location: Seaton Easy Devon UK

Post by computingfuture »

LiLpunkSkateR wrote:Phice, do you know how legal it would be to use these stocks/prices in a game that makes profit?

Now thats an idea. But how you you use it to make profit?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

computingfuture.net wrote:
LiLpunkSkateR wrote:Phice, do you know how legal it would be to use these stocks/prices in a game that makes profit?

Now thats an idea. But how you you use it to make profit?
I'm guessing he meant a game that sells account activation, etc that uses the data for it's game contents..?
Image Image
mybelle
Forum Newbie
Posts: 2
Joined: Thu Jun 10, 2010 3:23 pm

Re: Yahoo Stock Market retrieval system

Post by mybelle »

Thank you so much for this. I am a real amateur .. I found a stock ticker online, for a client who wanted it on a website I am building - here is the code:


It works great, but is missing the code to get the actual stock info. Can I use your script to modify this? If so, could I trouble you to tell me where to add it into this code? Thank you so much!
Last edited by mybelle on Thu Jun 10, 2010 5:25 pm, edited 2 times in total.
mybelle
Forum Newbie
Posts: 2
Joined: Thu Jun 10, 2010 3:23 pm

Re: Yahoo Stock Market retrieval system

Post by mybelle »

Thank you so much for this. I am a real amateur .. I found a stock ticker online, for a client who wanted it on a website I am building - here is the code:
<?php
///// Replace with Yahoo Finance stock quotes of your choice //////
$raw_stock_data = array('AAPL 200 205',
'BIDU 500 510','RIMM 150 155', 'SNDK 70 72',
'GOOG 500 510','MSFT 30 32','AMD 9 10');
sort($raw_stock_data);
//////////////////////////////////////////////////////////////////////////
?>
<div align="center">
<marquee bgcolor="#e0e1e1" direction="left" loop="20" width="100%%">
<?php
foreach ($raw_stock_data as $value) {
$results = explode(' ',$value);

$ticker = $results[0];
$buy = $results[1];
$sell = $results[2];

echo "<font color=\"#ffffff\">$ticker </font>";
echo "<font color=\"#00ff00\">$buy </font>";
echo "<font color=\"#ff0000\">$sell </font>";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
?>
It works great, but is missing the code to get the actual stock info. Can I use your script to modify this? If so, could I trouble you to tell me where to add it into this code? Thank you so much!
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Yahoo Stock Market retrieval system

Post by mikecampbell »

I put it all together into a ticker with live quotes. You can see (and run) the code here:

http://www.exorithm.com/algorithm/view/stock_ticker
uriljames
Forum Newbie
Posts: 1
Joined: Thu Mar 03, 2011 10:53 pm

Re: Yahoo Stock Market retrieval system

Post by uriljames »

I like your information very much about yahoo stock market retrieval system. Thanks for sharing this useful info.
Post Reply