我正在使用网上找到的蜜蜂来轻松获取股票价格:
https://keithweaverca.medium.com/using-a-stock-market-api-4ce65b7c67ac
我多次调用它必须完成一个列表,我已经将它插入到一个函数中。有时随机结果会给出错误:
未定义的属性:stdClass:
if($ticker != ''){
$API_KEY = "F54SPPZ6VDOEO5TX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=".$ticker."&apikey=" . $API_KEY));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$result = json_decode($server_output);
$date = new DateTime(date ("Y/m/d"));
$date ->sub(new DateInterval('P1D'));
$dataForAllDays = $result->{'Time Series (Daily)'};
$dataForSingleDate = $dataForAllDays->{$date ->format('Y-m-d')};
echo $dataForSingleDate->{'1. open'} . '<br/>';
} else{
echo "Errore API ";
}
} ```
The error is on the $ dataForAllDays, the lines are for getting yesterday's day (they work)