我一直在使用Yahoo Financial API從Yahoo下載歷史股票數據。據該網站報道,截至5月中旬,舊API已停止使用。已經有很多帖子討論了新呼叫的形式,例如:用於下載CSV文件的Yahoo API的PHP代碼
除了獲得碎塊方法:
但我必須誤解是什麼程序是因爲我總是得到一個錯誤,說它「無法打開流:HTTP請求失敗。HTTP/1.0 201 Unauthorized」。
以下是我的代碼。任何和所有的援助是受歡迎的。我不得不承認,我是一位古老的Fortran程序員,我的編碼反映了這一點。
良好的道路
比爾
$ticker = "AAPL";
$yahooURL="https://finance.yahoo.com/quote/" .$ticker ."/history";
$body=file_get_contents($yahooURL);
$headers=$http_response_header;
$icount = count($headers);
for($i = 0; $i < $icount; $i ++)
{
$istart = -1;
$istop = -1;
$istart = strpos($headers[$i], "Set-Cookie: B=");
$istop = strpos($headers[$i], "&b=");
if($istart > -1 && $istop > -1)
{
$Cookie = substr ($headers[$i] ,$istart+14,$istop - ($istart + 14));
}
}
$istart = strpos($body,"CrumbStore") + 22;
$istop = strpos($body,'"', $istart);
$Crumb = substr ($body ,$istart,$istop - $istart);
$iMonth = 1;
$iDay = 1;
$iYear = 1980;
$timestampStart = mktime(0,0,0,$iMonth,$iDay,$iYear);
$timestampEnd = time();
$url = "https://query1.finance.yahoo.com/v7/finance/download/".$ticker."?period1=".$timestampStart."&period2=".$timestampEnd."&interval=1d&events=history&crumb=".$Cookie."";
while (!copy($url, $newfile) && $iLoop < 10)
{
if($iLoop == 9) echo "Failed to download data." .$lf;
$iLoop = $iLoop + 1;
sleep(1);
}
的可能的複製[雅虎財經歷史數據下載網址不工作(https://開頭stackoverflow.com/questions/44044263/yahoo-finance-historical-data-downloader-url-is-not-working) –