How to get RealTime stock price using yahoo finance

给你一囗甜甜゛ 提交于 2019-12-03 08:04:45
<?php

class U_Yahoo{

    private function file_get_contents_curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_URL, $url);

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    //return the history quote from the simbol, default begin date is 90 day ago, the default end is today
    public function getHistoryQuote($symbol, $begin = 90, $end = 0){
        if(!$begin && !$end)
            $begin = $end = 0;

        $begin = Date('Y-m-d', strtotime("-{$begin} days"));
        $end = Date('Y-m-d', strtotime("-{$end} days"));
        $url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22$symbol%22%20and%20startDate%20%3D%20%22$begin%22%20and%20endDate%20%3D%20%22$end%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
        $jason_obj = json_decode( $this->file_get_contents_curl($url) );
        return $jason_obj->query->results->quote;
    }

    //return not just the quote but others informations too
    public function getCurrentData($symbol){
        $is_array = is_array($symbol);

        $imp_symbol = ($is_array)? implode('%22%2C%22', $symbol) : $symbol;

        $url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22$imp_symbol%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
        $jason_obj = json_decode( $this->file_get_contents_curl($url) );

        $result = $jason_obj->query->results->quote;

        return (is_array($symbol) and (count($symbol) == 1))? [$result] : $result;
    }

    //return all quotes from the param $symbol passed, if symbol is array, it will return other array indexed by the symbols
    public function getCurrentQuote($symbol){
        if(is_array($symbol)){
            $symbol = empty($symbol)? ['GOOG'] : $symbol;
            $data = $this->getCurrentData($symbol);
            $result = [];

            for ($c = 0; $c < count($data); $c++) { 
                $result[$data[$c]->Symbol] = $data[$c]->LastTradePriceOnly;
            }

            return $result;
        }else
            return $this->getCurrentData($symbol)->LastTradePriceOnly;
    }

}

How use:

$yahoo = new U_Yahoo();
var_dump( $yahoo->getCurrentQuote('GOOG') );
var_dump( $yahoo->getCurrentQuote(['GOOG', 'YHOO']) );

var_dump( $yahoo->getCurrentData(['GOOG', 'YHOO']) );

var_dump( $yahoo->getHistoryQuote('YHOO', 20, 0) );

well, in your approach, the stock price fetching is triggered by the client (the user's browser). So there is no way to trigger it outside page refresh or AJAX.

However, your server could fetch those data, irrespective of users. Something like:

data source <----> your backend server fetching the data ---> your database <---- your frontend web server <---> users

Backend and frontend servers can be the same server but with different processes.

check this web this may be what you want(Use it for realtime web application)

http://express-io.org/

http://socket.io/

Tutorials

http://blog.nodeknockout.com/post/34243127010/knocking-out-socket-io

As far as google is concerned I am pretty sure that the API is deprecated and is not working anymore. you can probably use Yahoo finance api, They have api for csv downloads and via yql.

Refer: https://code.google.com/p/yahoo-finance-managed/wiki/YahooFinanceAPIs

As far as realtime is conecrned, I suggest look at yahoo web services. Following is an example: http://finance.yahoo.com/webservice/v1/symbols/ITC.NS,ITC.BO/quote?format=json If you do not provide format, it will return a XML.

How would you make it realtime without refreshing or Ajax?

You can create a pubsub model and make your application subscribe to your application, you will have to create this layer since the yahoo api is pull based and not push based. So you will need to pull stock quotes from yahoo and push them to your application. You can probably use JMS for java or sockets, whichever suits you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!