json_decode 雅虎财经数据

json_decode yahoo finance data

我想从雅虎财经获取数据json url 是 https://query1.finance.yahoo.com/v7/finance/options/USD

我使用的代码是:

function get_finance_data(){

    $url ='https://query1.finance.yahoo.com/v7/finance/quote?symbols=USD';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch);
    curl_close($ch);
    $data = json_decode($result, true);
    return $data;
}

还有这个:

public function render()
{
    $financial_entity = array();
    $market_data = $this->get_finance_data();
                $item = array (
                    'symbol'          => $market_data['underlyingSymbol'],
                );
                $financial_entity[] = $item;
            

    var_dump($market_data);
    
}

var_dump($market_data); 我得到了内容

array(1) { 
  ["quoteResponse"]=> array(2) { 
    ["result"]=> array(1) { 
      [0] => array(59) { 
        ["language"]=> string(5) "en-US" ["region"]=> string(2) "US" ["quoteType"]=> string(3) "ETF" ["quoteSourceName"]=> string(13) "Delayed Quote" ["triggerable"]=> bool(true) ["currency"]=> string(3) "USD" ["ytdReturn"]=> float(15.65) ["trailingThreeMonthReturns"]=> float(15.89) ["trailingThreeMonthNavReturns"]=> float(15.65) ["fiftyDayAverage"]=> float(120.12118) ["fiftyDayAverageChange"]=> float(5.0788193) ["fiftyDayAverageChangePercent"]=> float(0.042280797) ["twoHundredDayAverage"]=> float(106.89956) ["twoHundredDayAverageChange"]=> float(18.300438) ["twoHundredDayAverageChangePercent"]=> float(0.17119282) ["sourceInterval"]=> int(15) ["exchangeDataDelayedBy"]=> int(0) ["tradeable"]=> bool(false) ["preMarketChange"]=> float(0) ["preMarketChangePercent"]=> float(0) ["preMarketTime"]=> int(1619428553) ["preMarketPrice"]=> float(125.2) ["regularMarketChange"]=> float(3.9300003) ["regularMarketChangePercent"]=> float(3.240703) ["regularMarketTime"]=> int(1619208000) ["regularMarketPrice"]=> float(125.2) ["regularMarketDayHigh"]=> float(126.1111) ["regularMarketDayRange"]=> string(17) "121.22 - 126.1111" ["regularMarketDayLow"]=> float(121.22) ["regularMarketVolume"]=> int(75592) ["regularMarketPreviousClose"]=> float(121.27) ["bid"]=> float(0) ["ask"]=> float(0) ["bidSize"]=> int(8) ["askSize"]=> int(8) ["fullExchangeName"]=> string(8) "NYSEArca" ["regularMarketOpen"]=> float(121.22) ["averageDailyVolume3Month"]=> int(60909) ["averageDailyVolume10Day"]=> int(51000) ["fiftyTwoWeekLowChange"]=> float(82.729996) ["fiftyTwoWeekLowChangePercent"]=> float(1.9479631) ["fiftyTwoWeekRange"]=> string(14) "42.47 - 136.25" ["fiftyTwoWeekHighChange"]=> float(-11.050003) ["fiftyTwoWeekHighChangePercent"]=> float(-0.08110094) ["fiftyTwoWeekLow"]=> float(42.47) ["fiftyTwoWeekHigh"]=> float(136.25) ["exchange"]=> string(3) "PCX" ["shortName"]=> string(30) "ProShares Ultra Semiconductors" ["longName"]=> string(30) "ProShares Ultra Semiconductors" ["messageBoardId"]=> string(14) "finmb_32431934" ["exchangeTimezoneName"]=> string(16) "America/New_York" ["exchangeTimezoneShortName"]=> string(3) "EDT" ["gmtOffSetMilliseconds"]=> int(-14400000) ["market"]=> string(9) "us_market" ["esgPopulated"]=> bool(false) ["firstTradeDateMilliseconds"]=> int(1170340200000) ["priceHint"]=> int(2) ["marketState"]=> string(3) "PRE" ["symbol"]=> string(3) "USD" 
      } 
    } 
    ["error"]=> NULL 
  } 
} 

但是我尝试 var_dump($financial_entity); 我得到了符号键的 NULL。

您请求的数据项 - $market_data['underlyingSymbol'] - 在示例数据中不存在。您需要使用正确的 属性 名称访问数据结构的正确部分:

'symbol' => $market_data['quoteResponse']['result'][0]['symbol'];