How to get RealTime-Aktienkurs yahoo finance

Ich habe mehrere Anforderungen, die nachstehend aufgeführt sind.

  1. Get Real-Time stock price, ohne die Seite aktualisieren oder ajax. (dh. yahoo Finanzen, Sie erhalten aktuelle Aktienkurs ohne Aktualisierung der Seite und ajax-call)
  2. Bekommen Aktienkurs aus, so viel wie Börse wie BSE, NSC usw..

Jetzt mit dem folgenden code bin ich in der Lage zu bekommen Aktienkurs aber entweder habe ich die Seite aktualisieren oder Aufruf von ajax und in beiden Fall, es nehmen 20 bis 30 Sekunden, aber in vielen Finanz-Website können Sie update-Preis von jede Sekunde ohne Verwendung von ajax.

<?php
/**
 * Class to fetch stock data from Yahoo! Finance
 *
 */

    class YahooStock {

        /**
         * Array of stock code
         */
        private $stocks = array();

        /**
         * Parameters string to be fetched  
         */
        private $format;

        /**
         * Populate stock array with stock code
         *
         * @param string $stock Stock code of company   
         * @return void
         */
        public function addStock($stock)
        {
            $this->stocks[] = $stock;
        }

        /**
         * Populate parameters/format to be fetched
         *
         * @param string $param Parameters/Format to be fetched
         * @return void
         */
        public function addFormat($format)
        {
            $this->format = $format;
        }

        /**
         * Get Stock Data
         *
         * @return array
         */
        public function getQuotes()
        {       
            $result = array();     
            $format = $this->format;

            foreach ($this->stocks as $stock)
            {           
                /**
                 * fetch data from Yahoo!
                 * s = stock code
                 * f = format
                 * e = filetype
                 */
                $s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=$format&e=.csv");

                /**
                 * convert the comma separated data into array
                 */
                $data = explode( ',', $s);

                /**
                 * populate result array with stock code as key
                 */
                $result[$stock] = $data;
            }
            return $result;
        }
    }
    $objYahooStock = new YahooStock;

    /**
        Add format/parameters to be fetched

        s = Symbol
        n = Name
        l1 = Last Trade (Price Only)
        d1 = Last Trade Date
        t1 = Last Trade Time
        c = Change and Percent Change
        v = Volume
     */
    $objYahooStock->addFormat("snl1d1t1cv");

    /**
        Add company stock code to be fetched

        msft = Microsoft
        amzn = Amazon
        yhoo = Yahoo
        goog = Google
        aapl = Apple   
     */
    $objYahooStock->addStock("msft");
    $objYahooStock->addStock("amzn");
    $objYahooStock->addStock("yhoo");
    $objYahooStock->addStock("goog");
    $objYahooStock->addStock("vgz");
    $objYahooStock->addStock("FB");
    /**
     * Printing out the data
     */
    ?>
    <table width="100%">
    <tr>
        <th>Row</th>
        <th>Code</th>
        <th>Name</th>
        <th>Last Trade Price</th>
        <th>Last Trade Time</th>
        <th>Change and Percent Change</th>
        <th>Volume</th>
    </tr>
    <?php
    foreach( $objYahooStock->getQuotes() as $code => $stock)
    {
        ?>
        <tr>
            <td><?php //print_r($stock); ?></td>
            <td><?php echo $stock[0]; ?></td>
            <td><?php echo $stock[1]; ?></td>
            <td><?php echo $stock[2]; ?></td>
            <td><?php echo $stock[3]; ?></td>
            <td><?php echo $stock[4]; ?></td>
            <td><?php echo $stock[5]; ?></td>
            <td><?php echo $stock[6]; ?></td>
        </tr>

        <?php
    }
?>
    </table>
soweit ich weiß, ohne die Seite aktualisieren oder AJAX können Sie die Daten vom server mit socket.io/#how-to-use

InformationsquelleAutor Dipen | 2014-01-17

Schreibe einen Kommentar