PDA

View Full Version : How to get stock data to a custom web page?


DreamTone
08-17-2008, 02:15 AM
Hello!

I would like to track some of my stocks and make a web page with the data but I am not sure how I would go about collecting this data?

Any ideas?

_Aerospace_Eng_
08-17-2008, 02:45 AM
First check to see if the have an rss feed of some kind or an api you could use. If they don't have one you'll need to use curl or something like file_get_contents and then you'll need to parse the data to get what you want. This is called web scraping and can be sometimes considered unethical.

Len Whistler
08-17-2008, 06:57 AM
Below is some php code from my code snippet folder. It gets live quotes from Yahoo Finance in real time. The array it retrieves has lots of stock info such as last trade, high/low, volume, etc. $quote[1] I think is last trade.

<html>
<body>
<h2>Real Time Yahoo Finance Stock Quotes With PHP</h2>
<?php

$ticker = "goog";

if (isset($_POST['get_quote'])) {
$ticker = $_POST['ticker'];
}

$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
$quote = fread($open, 2000);
fclose($open);
$quote = str_replace("\"", "", $quote);
$quote = explode(",", $quote);
$quote = ($quote[1]);

echo "Last trade for <b>$ticker</b> was <b>\$$quote</b>";

?>
<br><br>
<form method="post" action="<?php echo $PHP_SELF;?>">
Get Quote: <input type="text" size="10" maxlength="10" name="ticker"/>
<input type="submit" value="Get Quote" name="get_quote" />
</form>
Enter any valid stock quote such as:<br>
aapl<br>
hog<br>
rimm<br>
rht<br>
</body>
</html>


DEMO of the above script: http://www.604images.com/stock_quotes.php



----

DreamTone
08-17-2008, 03:37 PM
Hey Len, Thats great, I will give that a try. Is this something yahoo lets you do, if so how can I find out more about it, do they have a link that gives you basic instructions?

Though, It looks like with your example I have all I need now.

:thumbsup:


I am guessing on these after the first two ($$names) do you know what the others names are called?

"GD", 93.41, "8/15/2008", "4:01pm", +0.95, 91.01, 94.19, 91.01, 2659111
$ticker $$quote $$date?? $$time?? $$?? $$?? $$?? $$?? $$??

Len Whistler
08-17-2008, 05:26 PM
I assume it's OK from Yahoo to use the script. I have been using it for a few years to get currency quotes and I do credit Yahoo Finance with the data. I don't have any links to provide more info but this Yahoo Finance link should help: http://finance.yahoo.com/q?s=gd


GD

93.41 = Last Trade

8/15/2008 = Date EST

4:01pm = Last Trade

+0.95 = Change from previous day.

91.01 = Open

94.19 = Days High

91.01 = Days Low

2659111 = Days Volume



------------------

DreamTone
08-17-2008, 09:55 PM
HELP! :eek:

Ok, I am having some problems, because I am terrible at string manipulation and I assume what you are doing is placing the CSV into an array, then using the bellow code to chop out the part of the string you need. So can you post an example of how the bellow code works?


$quote = str_replace("\"", "", $quote);
$quote = explode(",", $quote);


Here was my try, but with not so good luck:


<html>
<body>
<h2>Real Time Yahoo Finance Stock Quotes With PHP</h2>
<?php

$ticker = "goog";

if (isset($_POST['get_quote'])) {
$ticker = $_POST['ticker'];
}

$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");

$quote = fread($open, 900);

fclose($open);

$tick = str_replace("\"", "", $quote);
$tick = explode(",", $quote);

$quote = str_replace("\"", "", $quote);
$quote = explode(",", $quote);

$date = str_replace("", "", $quote);
$date = explode("/",", $quote);

$time = str_replace("0", "", $quote);
$time = explode(",", $quote);


$quote = ($quote[1]);
$date = ($quote[2]);
$time = ($quote[3]);
$prevday = ($quote[4]);
$open = ($quote[5]);
$high = ($quote[6]);
$low = ($quote[7]);
$volume = ($quote[8]);

echo "Co: <b>\$$tick</b><br>";

echo "Last trade: <b>\$$quote</b><br>";

echo "Date: <b>\$$date</b><br>";

echo "Time: <b>\$$time</b><br>";

echo "Previous Day: <b>\$prevday</b><br>";

echo "Opened at: <b>\$$open</b><br>";

echo "Days High: <b>\$$high</b><br>";

echo "Days Low: <b>\$$low</b><br>";

echo "Days Volume: <b>\$$volume</b><br>";


?>
<br><br>
<form method="post" action="<?php echo $PHP_SELF;?>">
Get Quote: <input type="text" size="10" maxlength="10" name="ticker"/>
<input type="submit" value="Get Quote" name="get_quote" />
</form>
Enter any valid stock quote such as:<br>
aapl<br>
hog<br>
rimm<br>
rht<br>
</body>
</html>

Len Whistler
08-18-2008, 01:33 AM
The only line that needs changing to receive additional stock info is:

$quote = ($quote[1]);

Example:

$quote = ($quote[1]);
$quote_2 = ($quote[2]);
$quote_3 = ($quote[3]);


----------

DreamTone
08-18-2008, 04:29 AM
Great, I am back on track, and its working take a look:

http://www.dreamtone.org/test/quotes.htm

How do I get rid of the extra $ in the displayed output, it seems I need the two $$ to display the info or at least thats my thought so far?

echo "Date: <b>\$$quote_2</b><br>";

Here is all the php code now, how does it look so far?


<html>
<body>
<h2>Real Time Yahoo Finance Stock Quotes With PHP</h2>
<?php

$ticker = "goog";

if (isset($_POST['get_quote'])) {
$ticker = $_POST['ticker'];
}

$open = fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv", "r");
$quote = fread($open, 1000);

fclose($open);

$quote = str_replace("\"", "", $quote);
$quote = explode(",", $quote);

$quote_0 = ($quote[0]);
$quote_1 = ($quote[1]);
$quote_2 = ($quote[2]);
$quote_3 = ($quote[3]);
$quote_4 = ($quote[4]);
$quote_5 = ($quote[5]);
$quote_6 = ($quote[6]);
$quote_7 = ($quote[7]);
$quote_8 = ($quote[8]);

echo "Company: <b>\$$quote_0</b><br>";
echo "Last trade: <b>\$$quote_1</b><br>";
echo "Date: <b>\$$quote_2</b><br>";
echo "Time: <b>\$$quote_3</b><br>";
echo "From Previous: <b>\$$quote_4</b><br>";
echo "Open: <b>\$$quote_5</b><br>";
echo "High: <b>\$$quote_6</b><br>";
echo "Low: <b>\$$quote_7</b><br>";
echo "Volume: <b>\$$quote_8</b><br>";


?>
<br><br>
<form method="post" action="<?php echo $PHP_SELF;?>">
Get Quote: <input type="text" size="10" maxlength="10" name="ticker"/>
<input type="submit" value="Get Quote" name="get_quote" />
</form>
Enter any valid stock quote such as:<br>
aapl<br>
hog<br>
rimm<br>
rht<br>
</body>
</html>

Len Whistler
08-18-2008, 06:17 AM
How do I get rid of the extra $ in the displayed output, it seems I need the two $$ to display the info or at least thats my thought so far?

Not all values require the $, such as date, volume, ticker symbol. So delete the escaped $ (\$). The back slash is used to display characters that are also used by PHP for coding, such as: $ " '.


Change:
echo "Volume: <b>\$$quote_8</b><br>";
To:
echo "Volume: <b>$quote_8</b><br>";
Repeat on all lines that do not require the dollar sign.



----

DreamTone
08-19-2008, 01:06 AM
Thanks Len.

P.S. Like the beer page!