Zend - The PHP Company




Commerce

Add Code


Stock quote retrieval  

Type: class
Added by: jcostom
Entered: 04/03/2000
Last modified: 08/12/1999
Rating: ***** (14 votes)
Views: 12433
This class will, given a symbol, retrieve various pieces of information from Yahoo!'s stock quotes. It uses Yahoo's nifty .csv download option. It also works for the various indices, so long as you use the proper Yahoo! symbol for the index you want info on.


<?

/*

Copyright (c) 2000, Jason Costomiris
All rights reserved.

Don't be scared, it's just a BSD-ish license.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by Jason Costomiris.
4. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

    
Class quote {
        function 
get($symbol){
            
$url sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv"$symbol);
            
$fp fopen($url"r");
            if(!
$fp){
                echo 
"Error: Cannot connect";
            } else {
                
$array fgetcsv($fp4096',');
                
fclose($fp);
                
$this->symbol $array[0];
                
$this->last $array[1];
                
$this->date $array[2];
                
$this->time $array[3];
                
$this->change $array[4];
                
$this->open $array[5];
                
$this->high $array[6];
                
$this->low $array[7];
                
$this->volume $array[8];
            }
        }
    }
?>


Usage Example


<?
        
require("q.php");
        
$q = new quote;
        
$q->get("RHAT");
        
printf("%s: %s(%s)"$q->symbol$q->last$q->change);
?>


Rate This Script





Search



This Category All Categories