【发布时间】:2017-10-30 04:41:25
【问题描述】:
我一直在使用 Yahoo Financial API 从 Yahoo 下载历史股票数据。正如本网站所报道的,截至 5 月中旬,旧 API 已停止使用。有很多帖子针对新呼叫的形式,例如:
以及获取面包屑的方法:
但我一定是误解了程序是什么,因为我总是收到一条错误消息,说“无法打开流:HTTP 请求失败。HTTP/1.0 201 未授权”。
下面是我的代码。欢迎任何和所有的帮助。我不得不承认我是一个老 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);
}
【问题讨论】:
标签: php csv yahoo-finance