Пример #1
0
/**
 * Use this method to update our master list of stocks. The file supplied should
 * have one stock name per separator.
 * @param String $filename The filename including location
 * @param String $separator The separator used between each stock.
 * @return void Return nothing.
 **/
function update_stocks($filename, $separator)
{
    $file = file_get_contents($filename);
    $sArray = explode($separator, $file);
    //Lets first populate an array with all stocks from DB so we don't
    //have to make a DB call for each stock in file.
    $query = mysql_query("SELECT * FROM stocks");
    $current_stocks = array();
    while ($array = mysql_fetch_array($query)) {
        $current_stocks[$array['name']] = true;
    }
    //Now lets cycle through ones to add
    foreach ($sArray as $i => $value) {
        if (array_key_exists($value, $current_stocks) == false && $value != "") {
            echo "Adding " . $value . " <br />";
            $stock = new Stock(null);
            $stock->setName($value);
            $stock->persist();
        } else {
            echo "Skipped " . $value . "<br />";
        }
    }
}
Пример #2
0
/**
 * Will update details about stock including EPS/PPE/Stock Price/Capitilization etc...
 * from Yahoo Finance. Can call many times, but will only actually ping yahoo if its been
 * 16 hours since last update.
 * */
function update_stock_info($ticker)
{
    $stock = new Stock($ticker, false);
    if ($stock->getId() < 0) {
        return false;
    }
    //Only update stocks that haven't been updated in 16 hours.
    if (time() - $stock->getInfoDate() < TIME_HOUR * 16) {
        return false;
    }
    /**
     *s = stock, n = name, l1 = last trade, e = earnings per share, r = price per earings ration
     *x = stock exchange, t8 = 1 yr target price, j1 = market cap
     */
    $url = 'http://finance.yahoo.com/d/quotes.csv?s=' . $stock->getTicker() . '&f=snl1erxt8j1';
    //Set timeout for fetching url
    $ctx = stream_context_create(array('http' => array('timeout' => 3)));
    $file = @file_get_contents($url, 0, $ctx);
    if ($file == false) {
        return false;
    }
    /**
     *$array[0] = Ticker
     *$array[1] = Name
     *$array[2] = Last Trade Price
     *$array[3] = Earnings/Share
     *$array[4] = Price/Earnings
     *$array[5] = Stock Exchange
     *$array[6] = Year Target Price
     *$array[7] = Market Cap
     */
    //File should only be one line, explode into array
    $array = explode(',', $file);
    //No Name means stock doesn't exist.
    if ($array[1] == "") {
        $stock->setStatus(STATUS_INACTIVE);
        $stock->persist();
        return false;
    }
    $stock->setName(addslashes(str_replace('"', '', $array[1])));
    $stock->setLastTrade($array[2]);
    $stock->setEarningsPerShare($array[3]);
    $stock->setPricePerEarnings($array[4]);
    $array[5] = strtoupper(str_replace('"', '', $array[5]));
    if ($array[5] == "") {
        //5 is our index N/A
        $stock->setStockExchangeId(5);
    } else {
        //Check to see if stock exchange exists, if it doesnt insert into database.
        $query = mysql_query("SELECT * FROM exchanges WHERE ticker = '" . $array[5] . "'");
        if (mysql_num_rows($query) < 1) {
            mysql_query("INSERT INTO exchanges\r\n                        (ticker)\r\n                        VALUES\r\n                        ('" . $array[5] . "')");
            $stock->setStockExchangeId(mysql_insert_id());
        } else {
            $mysql_array = mysql_fetch_array($query);
            $stock->setStockExchangeId($mysql_array['id']);
        }
    }
    $stock->setTargetPrice($array[6]);
    //Get rid of any whitespace
    $array[7] = trim($array[7]);
    if (substr($array[7], strlen($array[7]) - 1, 1) == 'M') {
        $stock->setMarketCap($array[7] / 1000);
    } else {
        if (substr($array[7], strlen($array[7]) - 1, 1) == 'K') {
            $stock->setMarketCap($array[7] / 1000000);
        } else {
            $stock->setMarketCap($array[7] / 1);
        }
    }
    //Set time of this update, so we dont' do it again for another 16 hrs minimum.
    $stock->setInfoDate(time());
    $stock->persist();
    return true;
}