2015年4月29日 星期三

PHP:取得股票過去記錄

昨天提到的方法是獲取當刻的股票報價,對於收集過往績效沒有幫助。這樣要使用另一個查詢接口:
<?php

$stock = "2628.HK"
$startDate = "2015-04-27";
$endDate = $startDate;

$api = "https://query.yahooapis.com/v1/public/yql?q=".
       urlencode("select * from yahoo.finance.historicaldata").
       urlencode(" where symbol in ('$stock') AND startDate='$startDate' AND endDate='$endDate'").
       "&format=json&diagnostics=true".
       "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";

//-----------------------------------------------------------------------------
try  {
    $rawData = file_get_contents($api);
    $jsonData = json_decode($rawData);

    $price = $jsonData->query->results->quote->Close;
    echo("Price: ".$price);

}  catch (Exception $e)  {
    echo($e->getMessage();)
}

?>

以下是傳回的結果:
{
    "query":{
        "count":1,
        "created":"2015-04-29T13:20:58Z",
        "lang":"en-US",
        "diagnostics":{
            "url":[{
                "execution-start-time":"0",
                "execution-stop-time":"1",
                "execution-time":"1",
                "content":"http://www.datatables.org/yahoo/finance/yahoo.finance.historicaldata.xml"
            },
            {
                "execution-start-time":"7",
                "execution-stop-time":"20",
                "execution-time":"13",
                "content":"http://ichart.finance.yahoo.com/table.csv?a=3&b=27&e=27&g=d&c=2015&d=3&f=2015&s=2628.HK"
            },
            {
                "execution-start-time":"23",
                "execution-stop-time":"34",
                "execution-time":"11",
                "content":"http://ichart.finance.yahoo.com/table.csv?a=3&b=27&e=27&g=d&c=2015&d=3&f=2015&s=2628.HK"
            }],
            "publiclyCallable":"true",
            "cache":[{
                "execution-start-time":"6",
                "execution-stop-time":"6",
                "execution-time":"0",
                "method":"GET",
                "type":"MEMCACHED",
                "content":"bc71a240dc64a8699677be3a0ddf89f6"
            },
            {
                "execution-start-time":"22",
                "execution-stop-time":"22",
                "execution-time":"0",
                "method":"GET",
                "type":"MEMCACHED",
                "content":"acdc182ddaac86c188221ed1cc78e37b"
            }],
            "query":[{
                "execution-start-time":"6",
                "execution-stop-time":"22",
                "execution-time":"16",
                "params":"{url=[http://ichart.finance.yahoo.com/table.csv?a=3&b=27&e=27&g=d&c=2015&d=3&f=2015&s=2628.HK]}",
                "content":"select * from csv(0,1) where url=@url"
            },
            {
                "execution-start-time":"23",
                "execution-stop-time":"34",
                "execution-time":"11",
                "params":"{columnsNames=[Date, Open, High, Low, Close, Volume, Adj_Close], url=[http://ichart.finance.yahoo.com/table.csv?a=3&b=27&e=27&g=d&c=2015&d=3&f=2015&s=2628.HK]}",
                "content":"select * from csv(2,0) where url=@url and columns=@columnsNames
            }],
            "javascript":{
                "execution-start-time":"5",
                "execution-stop-time":"35",
                "execution-time":"30",
                "instructions-used":"32648",
                "table-name":"yahoo.finance.historicaldata"
            },
            "user-time":"35",
            "service-time":"25",
            "build-version":"0.2.100"
        },
        "results":{
            "quote":{
                "Symbol":"2628.HK",
                "Date":"2015-04-27",
                "Open":"37.95",
                "High":"38.30",
                "Low":"37.50",
                "Close":"38.20",
                "Volume":"45063300",
                "Adj_Close":"38.20"
            }
        }
    }
}

沒有留言: