Version: 1.00.00.0004
Type: Class
Category: Money
License: GNU General Public License
Description: This class will retrieve information from Yahoo’s finance site and put it into a nice, usable, class variable for you to use elsewhere at your leisure. Example usage is in the class comments.
<?php // c.ticker.php // $version: 1.00.00.0004 // // A portion of the phpcmx system under development by J. Marshall Presnell // GPL 2001 J Marshall Presnell ([email protected]) // // Please let me know if you update this class or use it in your systems. // // ------------------------------------------------------------------------- // // Retrieve information from Yahoo finance on a specific stock ticker symbol // // Usage example: // // $s = new ticker(); // $s->get("MSFT"); // if ($s->lastTrade > $s->open) { // print ("Bill made money today"); // } // // Note that this class is specifically designed to NOT do the fancy stuff // like displaying the data, that is up to the calling program. // // Some will undoubtedly question why I put this simple task in a class, // well.... it's because I plan to expand the class with more sophisticated // lookups including news, analyst's recommendations, and SEC filings. // // ------------------------------------------------------------------------- // Prevent multiple inclusions so it is safe to include it pretty much anywhere. if (!defined("C_TICKER_PHP")) { define("C_TICKER_PHP", 1); class ticker { var $ticker; var $lastTrade; var $lastTradeTime; var $change; var $open; var $dayRange; var $high; var $low; var $volume; var $chartURL; // ======================================== // Constructor // ======================================== function ticker() { // No constructor requirements } function get($symbol) { // First, clear everything out... $this->ticker = ""; $this->lastTrade = ""; $this->lastTradeTime = ""; $this->change = ""; $this->open = ""; $this->dayRange = ""; $this->volume = ""; $this->chartURL = ""; // Get the CSV data from the finance.yahoo.com site, do a quick open/read/close // operation. (Not too critical for low volume sites, but higher volume sites // will notice some small efficiency increase) $fp = fopen("http://quote.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv", "r"); $read = fread($fp, 2048); fclose($fp); // Remove all the quotes from the information $read = str_replace(""", "", $read); // Parse out the fields on comma boundaries to the $read array $read = explode(",", $read); // Now, populate the member variables $this->ticker = $read[0]; $this->lastTrade = $read[1]; $this->lastTradeTime = $read[2] . " " . $read[3]; $this->open = $read[5]; $this->high = $read[6]; $this->low = $read[7]; $this->dayRange = $read[7] . " - " . $read[6]; $this->volume = $read[8]; $this->change = $this->lastTrade - $this->open; $this->chartURL ="http://ichart.yahoo.com/t?s=$this->ticker"; } } } ?>