Location: PHPKode > scripts > Yahoo Quotes > yahoo-quotes/yahooquotes.php
<?php
// Class YahooQuotes holt Kursdaten von finance.yahoo.com
// ---------------------------------------------------------------------
// va 20080930 start
// va 20081005 __construct, getHistQuotes, getDayQuotes
// ---------------------------------------------------------------------

class YahooQuotes {

	var $sym;         // StockSymbol e.g. MSFT
	var $from;        // Date from array (YYYY, M, D)
	var $to;          // Date to   array (YYYY, M, D)
	var $histurl;     // URL to retrieve historical prices and volume
	var $dayurl;      // URL to retrieve intraday data
	var $histquotes;  // Array containing retreived historical data
	var $dayquote;    // Array containing retrieved intraday data
	var $ERR;         // String containing ErrorMessage
	
	function __construct($sym="", $datefrom=array(), $dateto=array()) {
		$this->ERR = "";
		if (!$sym) return false;
			else $this->sym = strtoupper ($sym);
		if ($datefrom) {
			$this->from = $datefrom;
		} else {
			$this->from = array (1910, 1, 1);
		}
		if ($dateto) {
			$this->to = $dateto;
		} else {
			$this->to = array (date ("Y"), date ("m"), date ("d"));
		}
	}

	// HISTORICAL PRICES
	// URL: http://hart.finance.yahoo.com/table.csv?s=MSFT&a=01&b=10&c=2008&d=02&e=10&f=2008&g=d&ignore=.csv
  // a= startmonth-1    b= startday    c= startyear
	// d= endmonth-1      e= endday      f= endyear
	// &g=d for daily prices
	// RETURNS:
	// Date,Open,High,Low,Close,Volume,Adj Close
	// 2008-05-21,22.13,22.28,21.01,21.06,113449100,20.70
	
	function getHistQuotes() {
		$this->histurl = "http://ichart.finance.yahoo.com/table.csv?s=".$this->sym.
			"&a=".($this->from[1]-1).
			"&b=".$this->from[2].
			"&c=".$this->from[0].
			"&d=".($this->to[1]-1).
			"&e=".$this->to[2].
			"&f=".$this->to[0].
			"&g=d&ignore=.csv";
		
		$rawdata="";
		if (!(@$fd = fopen ($this->histurl, "r"))) {
			$this->ERR = "ERROR: cannot open url ".$this->histurl."\n"; 
			return false;
		}
		while (!feof($fd)) {
  	  $rawdata .= fread($fd, 8192);
  	}
		fclose ($fd);
    
  	$this->histquotes= array();  
    if ($rawdata = trim ($rawdata)) {
    	$i=0;
    	foreach (explode ("\n", $rawdata) as $line) {
    		if (!$i++) {
    			if (!(substr(strtoupper($line), 0, 10) == "DATE,OPEN,")) return false;
				} else {
					$this->histquotes[$i-2]= explode (",", $line);
				}
			}
			if ($i>1) return true;
		}
		$this->ERR = "ERROR: No valid data from ".$this->histurl."\n"; 
		return false;
	}
	
	
	// TODAY'S PRICES
	// URL: http://download.finance.yahoo.com/d/quotes.csv?s=MSFT&f=sl1d1t1c1ohgv&e=.csv
	// RETURNS:
	// "MSFT",26.32,"10/3/2008","4:00pm",+0.07,26.39,27.47,26.24,116068448
	// pretty lousy format imho
	
	function getTodayQuotes() {
		$this->dayurl = "http://download.finance.yahoo.com/d/quotes.csv?s=".$this->sym."&f=sl1d1t1c1ohgv&e=.csv";
		if (!(@$fd = fopen ($this->dayurl, "r"))) {
			$this->ERR = "ERROR: cannot open url ".$this->dayurl."\n"; 
			return false;
		}
		$rawdata= fread($fd, 1000);
    fclose ($fd);
    
    if (!($rawdata = trim (str_replace ("\"","",$rawdata)))) return false;
    $seg= explode (",", $rawdata);
    $dtemp= explode("/", $seg[2]);
    if (sizeof($dtemp) != 3) {
			$this->ERR = "ERROR: No valid data from ".$this->dayurl."\n"; 
			return false;
		}
    $this->dayquote= array();
		$this->dayquote[0] = date("Y-m-d", mktime(0,0,0,$dtemp[0],$dtemp[1],$dtemp[2]));
		$this->dayquote[1] = $seg[5];
		$this->dayquote[2] = $seg[6];
		$this->dayquote[3] = $seg[7];
		$this->dayquote[4] = $seg[1];
		$this->dayquote[5] = $seg[8];
		$this->dayquote[6] = $seg[1];
		$this->dayquote["time"] = $seg[3];
		$this->dayquote["ptchange"] = $seg[4];
		return true;
	}
}

?> 
Return current item: Yahoo Quotes