Zend - The PHP Company




Commerce

Add Code


Multiple stock quote retrieval  

Type: class
Added by: gren
Entered: 09/04/2000
Last modified: 08/12/1999
Rating: **** (4 votes)
Views: 9378
I messed around with the stock quote retrieval class, and made the get() function in it to get multiple quotes rather than just a single one. Example here


<?php
    
// put this class in a file called quote.inc if you use the example below
    
class quote {
        function 
get($symbols){
            
$url sprintf('http://finance.yahoo.com/d/quotes.csv?s=%s&f=sl1d1t1c1ohgv'implode(','$symbols));
            
$fp fopen($url'r');
            if(!
$fp){
                
$this->StockQuote[0]['ERROR'] = 1;
            } else {
                
$x 1;
                while(
$row fgetcsv($fp1000',')){
                    
$this->StockQuote[$x]['symbol'] = trim(preg_replace('/"/'''$row[0]));
                    
$this->StockQuote[$x]['last'] = trim($row[1]);
                    
$this->StockQuote[$x]['date'] = trim(preg_replace('/"/'''$row[2]));
                    
$this->StockQuote[$x]['time'] = trim(preg_replace('/"/'''$row[3]));
                    
$this->StockQuote[$x]['change'] = trim($row[4]);
                    
$this->StockQuote[$x]['open'] = trim($row[5]);
                    
$this->StockQuote[$x]['high'] = trim($row[6]);
                    
$this->StockQuote[$x]['low'] = trim($row[7]);
                    
$this->StockQuote[$x]['volume'] = trim($row[8]);
                    
$x++;
                }
                
fclose($fp);
                
$this->StockQuote[0]['CVS_URL'] = $url;
                
$this->StockQuote[0]['SYMBOL_COUNT'] = ($x 1);
            }
        }
    }
?>


Usage Example


<?php
require('quote.inc');
$sqa = (isset($sqa)) ? explode(','$sqa) : array('MSFT''YHOO''RHAT');
$q = new quote;
$q->get($sqa);
?>
<font size="2" face="Arial, Helvetica">
<?php if($q->StockQuote[0]['ERROR'] != 1): ?>
<?php 
for($i=1;$i<=$q->StockQuote[0]['SYMBOL_COUNT'];$i++): ?>
<b><?=$q->StockQuote[$i]['symbol']?></b>
<font size="1"><?=$q->StockQuote[$i]['last']?></font><br>
<?php endfor; ?>
<?php 
else: ?>
<b>Stock quote retrieval error.</b><br>
<?php endif; ?>
</font>
<form action="<?=$PHP_SELF?>" method="GET">
<input type="text" name="sqa" value="<?=implode(','$sqa)?>">
<input type="submit" value="Get Quotes">
</form>


Rate This Script





Search



This Category All Categories