使用 php 从 json url 解析股票的高价 yahoo.finance.historicaldata

parse from json url the high price of a stock from yahoo.finance.historicaldata using php

我正在学习如何解析 json 文件...无法找出我的错误..尝试在图表上绘制这些数据

$url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in(%22TATASTEEL.NS%22)%20and%20startDate%3D%222015-07-01%22%20and%20endDate%3D%222015-07-30%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";

$output = file_get_contents($url);

$jdecode=json_decode($output,true);

foreach ($jdecode[query][results][quote][Open] as $key => $value) {
    # code...
    echo $key." : ".$value."<br>";
}

例如:当我在 yql 控制台中输入此查询时:

select * 来自 yahoo.finance.historicaldata 其中符号在 ("TATASTEEL.NS") 和 startDate = "2014-03-01" 和 endDate = "2015-03-30"

我得到一个 json link 这样的:

'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22TATASTEEL.NS%22)%20and%20startDate%20%3D%20% 222014-03-01%22%20and%20endDate%20%3D%20%222015-03-30%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback='

我如何解析这个 json...

试试这个:如果你还想 return 高价股票数组

    $json = '{"query":{"count":22,"created":"2015-09-08T03:16:02Z","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":"9","execution-stop-time":"31","execution-time":"22","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"},{"execution-start-time":"34","execution-stop-time":"45","execution-time":"11","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"}],"publiclyCallable":"true","cache":[{"execution-start-time":"8","execution-stop-time":"8","execution-time":"0","method":"GET","type":"MEMCACHED","content":"849df977aa5fea49fe77822dc0412ba5"},{"execution-start-time":"33","execution-stop-time":"34","execution-time":"1","method":"GET","type":"MEMCACHED","content":"28e5f7f76a8c1b5cf5932f92c807c420"}],"query":[{"execution-start-time":"8","execution-stop-time":"33","execution-time":"25","params":"{url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(0,1) where url=@url"},{"execution-start-time":"34","execution-stop-time":"46","execution-time":"12","params":"{columnsNames=[Date,Open,High,Low,Close,Volume,Adj_Close], url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(2,0) where url=@url and columns=@columnsNames"}],"javascript":{"execution-start-time":"6","execution-stop-time":"64","execution-time":"57","instructions-used":"242787","table-name":"yahoo.finance.historicaldata"},"user-time":"65","service-time":"35","build-version":"0.2.154"},"results":{"quote":[{"Symbol":"TATASTEEL.NS","Date":"2015-07-30","Open":"252.90","High":"253.00","Low":"247.55","Close":"248.15","Volume":"5169000","Adj_Close":"248.15"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-29","Open":"251.00","High":"254.65","Low":"249.40","Close":"250.55","Volume":"4003400","Adj_Close":"250.55"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-28","Open":"251.00","High":"252.45","Low":"246.40","Close":"249.25","Volume":"5538500","Adj_Close":"249.25"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-27","Open":"263.00","High":"263.20","Low":"250.05","Close":"250.75","Volume":"7648400","Adj_Close":"250.75"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-24","Open":"269.70","High":"271.65","Low":"264.20","Close":"265.05","Volume":"3810200","Adj_Close":"265.05"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-23","Open":"274.00","High":"274.45","Low":"269.30","Close":"269.90","Volume":"6961200","Adj_Close":"269.90"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-22","Open":"271.25","High":"281.00","Low":"268.70","Close":"280.40","Volume":"5910800","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-21","Open":"284.10","High":"284.10","Low":"273.85","Close":"274.70","Volume":"5095100","Adj_Close":"266.8626"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-20","Open":"285.00","High":"287.70","Low":"283.00","Close":"283.55","Volume":"3733700","Adj_Close":"275.4601"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-17","Open":"282.00","High":"286.35","Low":"280.00","Close":"284.05","Volume":"6449900","Adj_Close":"275.9459"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-16","Open":"285.10","High":"286.50","Low":"280.10","Close":"280.80","Volume":"6313300","Adj_Close":"272.7886"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-15","Open":"282.70","High":"284.80","Low":"279.10","Close":"280.40","Volume":"3679700","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-14","Open":"284.75","High":"286.70","Low":"280.50","Close":"281.15","Volume":"3430700","Adj_Close":"273.1286"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-13","Open":"283.50","High":"285.70","Low":"279.00","Close":"284.45","Volume":"3436800","Adj_Close":"276.3345"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-10","Open":"282.90","High":"284.50","Low":"278.75","Close":"280.90","Volume":"4021300","Adj_Close":"272.8857"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-09","Open":"283.50","High":"287.40","Low":"279.60","Close":"280.50","Volume":"6214700","Adj_Close":"272.4972"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-08","Open":"294.00","High":"294.00","Low":"281.80","Close":"283.15","Volume":"8141900","Adj_Close":"275.0715"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-07","Open":"301.70","High":"303.05","Low":"296.10","Close":"298.20","Volume":"3298700","Adj_Close":"289.6922"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-06","Open":"295.00","High":"299.75","Low":"292.00","Close":"299.00","Volume":"3708100","Adj_Close":"290.4693"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-03","Open":"306.35","High":"307.90","Low":"299.55","Close":"301.20","Volume":"2742500","Adj_Close":"292.6066"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-02","Open":"307.25","High":"310.45","Low":"303.20","Close":"304.35","Volume":"3840100","Adj_Close":"295.6667"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-01","Open":"305.00","High":"309.00","Low":"303.40","Close":"306.95","Volume":"3143800","Adj_Close":"298.1925"}]}}}';
    $a = json_decode($json, true);


    $high_stocks = [];
    foreach($a['query']['results']['quote'] as $key => $val)
    {
        $high_stocks = $val['High'];
    }
    print_r($high_stocks);

或者如果你只想回显它。

    $json = '{"query":{"count":22,"created":"2015-09-08T03:16:02Z","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":"9","execution-stop-time":"31","execution-time":"22","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"},{"execution-start-time":"34","execution-stop-time":"45","execution-time":"11","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"}],"publiclyCallable":"true","cache":[{"execution-start-time":"8","execution-stop-time":"8","execution-time":"0","method":"GET","type":"MEMCACHED","content":"849df977aa5fea49fe77822dc0412ba5"},{"execution-start-time":"33","execution-stop-time":"34","execution-time":"1","method":"GET","type":"MEMCACHED","content":"28e5f7f76a8c1b5cf5932f92c807c420"}],"query":[{"execution-start-time":"8","execution-stop-time":"33","execution-time":"25","params":"{url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(0,1) where url=@url"},{"execution-start-time":"34","execution-stop-time":"46","execution-time":"12","params":"{columnsNames=[Date,Open,High,Low,Close,Volume,Adj_Close], url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(2,0) where url=@url and columns=@columnsNames"}],"javascript":{"execution-start-time":"6","execution-stop-time":"64","execution-time":"57","instructions-used":"242787","table-name":"yahoo.finance.historicaldata"},"user-time":"65","service-time":"35","build-version":"0.2.154"},"results":{"quote":[{"Symbol":"TATASTEEL.NS","Date":"2015-07-30","Open":"252.90","High":"253.00","Low":"247.55","Close":"248.15","Volume":"5169000","Adj_Close":"248.15"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-29","Open":"251.00","High":"254.65","Low":"249.40","Close":"250.55","Volume":"4003400","Adj_Close":"250.55"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-28","Open":"251.00","High":"252.45","Low":"246.40","Close":"249.25","Volume":"5538500","Adj_Close":"249.25"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-27","Open":"263.00","High":"263.20","Low":"250.05","Close":"250.75","Volume":"7648400","Adj_Close":"250.75"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-24","Open":"269.70","High":"271.65","Low":"264.20","Close":"265.05","Volume":"3810200","Adj_Close":"265.05"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-23","Open":"274.00","High":"274.45","Low":"269.30","Close":"269.90","Volume":"6961200","Adj_Close":"269.90"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-22","Open":"271.25","High":"281.00","Low":"268.70","Close":"280.40","Volume":"5910800","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-21","Open":"284.10","High":"284.10","Low":"273.85","Close":"274.70","Volume":"5095100","Adj_Close":"266.8626"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-20","Open":"285.00","High":"287.70","Low":"283.00","Close":"283.55","Volume":"3733700","Adj_Close":"275.4601"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-17","Open":"282.00","High":"286.35","Low":"280.00","Close":"284.05","Volume":"6449900","Adj_Close":"275.9459"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-16","Open":"285.10","High":"286.50","Low":"280.10","Close":"280.80","Volume":"6313300","Adj_Close":"272.7886"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-15","Open":"282.70","High":"284.80","Low":"279.10","Close":"280.40","Volume":"3679700","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-14","Open":"284.75","High":"286.70","Low":"280.50","Close":"281.15","Volume":"3430700","Adj_Close":"273.1286"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-13","Open":"283.50","High":"285.70","Low":"279.00","Close":"284.45","Volume":"3436800","Adj_Close":"276.3345"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-10","Open":"282.90","High":"284.50","Low":"278.75","Close":"280.90","Volume":"4021300","Adj_Close":"272.8857"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-09","Open":"283.50","High":"287.40","Low":"279.60","Close":"280.50","Volume":"6214700","Adj_Close":"272.4972"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-08","Open":"294.00","High":"294.00","Low":"281.80","Close":"283.15","Volume":"8141900","Adj_Close":"275.0715"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-07","Open":"301.70","High":"303.05","Low":"296.10","Close":"298.20","Volume":"3298700","Adj_Close":"289.6922"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-06","Open":"295.00","High":"299.75","Low":"292.00","Close":"299.00","Volume":"3708100","Adj_Close":"290.4693"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-03","Open":"306.35","High":"307.90","Low":"299.55","Close":"301.20","Volume":"2742500","Adj_Close":"292.6066"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-02","Open":"307.25","High":"310.45","Low":"303.20","Close":"304.35","Volume":"3840100","Adj_Close":"295.6667"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-01","Open":"305.00","High":"309.00","Low":"303.40","Close":"306.95","Volume":"3143800","Adj_Close":"298.1925"}]}}}';
    $a = json_decode($json, true);

    foreach($a['query']['results']['quote'] as $key => $val)
    {
        echo 'High Stocks: ' . $val['High'] . '<br/>';
    }