May 19, 2000 Goes to Yahoo and gets stock quote information. Returns an associative array (or whatever they are called in PHP) containing the data: symbol, price, last_trade_date, last_trade_time, change, previous_trade_price, high, low, volume, cached, updated */ // Configuration Variables $tmpdir = "/home/chris/public_html/tmp"; $cachetime = 300; $symbol=strtoupper($symbol); $data["updated"]=filemtime("$tmpdir/$symbol"); if (!$tmpdir || ($data["updated"] < (time() - $cachetime))) { $fp=fopen("http://finance.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv","r"); $raw_line=fgets($fp,4096); fclose($fp); if ($tmpdir) { $fp=fopen("$tmpdir/$symbol","w"); fputs($fp,$raw_line); fclose($fp); } $data["updated"]=time(); } else { // Use the cached copy $data["cached"]=1; $fp=fopen("$tmpdir/$symbol","r"); $raw_line=fgets($fp,4096); fclose($fp); } // massage the data and return it $raw_array=explode(",",$raw_line); $data["symbol"]=ereg_replace("\"","",$raw_array[0]); $data["price"]=$raw_array[1]; $data["last_trade_date"]=ereg_replace("\"","",$raw_array[2]); $data["last_trade_time"]=ereg_replace("\"","",$raw_array[3]); $data["change"]=$raw_array[4]; if ($data["change"] >0) { $data["direction"]="up"; } elseif ($data["change"] <0) { $data["direction"]="down"; } else { $data["direction"]="flat"; } $data["previous_trade_price"]=$raw_array[5]; $data["high"]=$raw_array[6]; $data["low"]=$raw_array[7]; $data["volume"]=ereg_replace("[\n\r]","",$raw_array[8]); if ($data["price"]) { return $data; } } ?>
examples:
notes:Quotes are delayed up to 20 minutes. Also, results are cached for 300 seconds for speed. This is customizable. You can also see the source code to this thingie.