PHPRO.ORG

Yahoo Stock Report

Yahoo Stock Report

This is a very simple class that will fetch stock market reports from Yahoo. This could be done with a simple array but here a nice OO interface is provided.

The script itself returns an array of arrays containing various data about a given stock. The data in each array contains the following information.

  • NasdaqGS: MSFT
  • Last Traded
  • Last Traded Date
  • Last Traded Time
  • Change
  • Open
  • Days Range High
  • Days Rang Low
  • Stocks Traded

<?php

/**
 * Class to fetch Yahoo Stock results
 *
 * @copyright Copyright (C) 2008 PHPRO.ORG. All rights reserved.
 *
 * @license http://phpro.org/licenses/new_bsd New BSD License
 *
 * @version //autogentag//
 *
 * @filesource
 */

class proStock {

    
/*
     * @the array of stocks
     */
    
private $stocks = array();

    
/**
    *
    * @add a stock
    *
    * @access public
    *
    * @param string $stock The name of the stock
    *
    * @return void
    *
    */
    
public function addStock($stock)
    {
        
$this->stocks[] = $stock;
    }

    
/*
    *
    * @get the quotes for the stocks
    *
    * @access public
    *
    * @return array
    *
    */
    
public function getQuotes()
    {
        
/*** the return array ***/
        
$ret = array();

        
/*** loop over the stocks ***/
        
foreach ($this->stocks as $stock)
        {
            
/*** s=stock f=format e=filetype ***/
            
$s file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1d1t1c1ohgv&e=.csv");
            
/*** make an array of the data ***/
            
$data explode','$s);

            
$ret[$stock] = $data;
        }
        return 
$ret;
    }


/*** end of class ***/

Example Usage


<?php

$fin 
= new proStock;

$fin->addStock("msft");
$fin->addStock("amzn");
$fin->addStock("yhoo");
$fin->addStock("goog");

foreach( 
$fin->getQuotes() as $stock)
{
    echo 
$stock[0].' - '$stock[1] . '<br />';
}
?>

Demonstration

-
-
-
-