我一直在使用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) –