2017-05-29 75 views
0

我一直在使用Yahoo Financial API從Yahoo下載歷史股票數據。據該網站報道,截至5月中旬,舊API已停止使用。已經有很多帖子討論了新呼叫的形式,例如:用於下載CSV文件的Yahoo API的PHP代碼

https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=315561600&period2=1496087439&interval=1d&events=history&crumb=XXXXXXXXXXX

除了獲得碎塊方法:

​​

但我必須誤解是什麼程序是因爲我總是得到一個錯誤,說它「無法打開流: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); 
} 
+1

的可能的複製[雅虎財經歷史數據下載網址不工作(https://開頭stackoverflow.com/questions/44044263/yahoo-finance-historical-data-downloader-url-is-not-working) –

回答

0

@Craig Cocca這是不完全重複的,因爲你給的參考值給出Python中的解決方案,它對於我們這些誰使用PHP,但沒有學習python並沒有多大幫助。我很樂意看到解決方案與PHP。我已經檢查了雅虎頁面,並能夠提取碎片,但無法解決如何將它放入流和GET調用中。 我最新的(失敗)的努力是:

 $headers = [ 
     "Accept" => "*/*", 
     "Connection" => "Keep-Alive", 
     "User-Agent" => sprintf("curl/%s", curl_version()["version"])  
    ]; 

    // open connection to Yahoo 
    $context = stream_context_create([ 
     "http" => [ 
      "header" => (implode(array_map(function($value, $key) { return sprintf("%s: %s\r\n", $key, $value); }, $headers, array_keys($headers))))."Cookie: $Cookie", 
      "method" => "GET" 
     ] 
    ]); 
    $handle = @fopen("https://query1.finance.yahoo.com/v7/finance/download/{$symbol}?period1={$date_now}&period2={$date_now}&interval=1d&events=history&crumb={$Crumb}", "r", false, $context); 
    if ($handle === false) 
    { 
     // trigger (big, orange) error 
     trigger_error("Could not connect to Yahoo!", E_USER_ERROR); 
     exit; 
    } 

    // download first line of CSV file 
    $data = fgetcsv($handle); 

兩個日期都是UNIX編碼日期即:$ date_now =的strtotime($日期);

0

我已經設法下載股價歷史。目前我只是拿現在的價格數字,但我的下載方法收到過去一年的歷史數據。 (即直到雅虎決定在數據上添加其他數據塊)。 我的解決方案使用了我添加到我的/ includes文件夾中的「simple_html_dom.php」解析器。 下面是代碼(從哈佛大學CS50課程的原始版本,我建議初學者像我這樣的修改):

function lookup($symbol) 
{ 
// reject symbols that start with^
    if (preg_match("/^\^/", $symbol)) 
    { 
     return false; 
    } 
// reject symbols that contain commas 
    if (preg_match("/,/", $symbol)) 
    { 
     return false; 
    } 
    // body of price history search 
$sym = $symbol; 
    $yahooURL='https://finance.yahoo.com/quote/'.$sym.'/history?p='.$sym; 

// get stock name 
$data = file_get_contents($yahooURL); 
    $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $data, $matches) ? $matches[1] : null; 

$title = preg_replace('/[[a-zA-Z0-9\. \| ]* \| /','',$title); 
$title = preg_replace('/ Stock \- Yahoo Finance/','',$title); 
$name = $title; 

// get price data - use simple_html_dom.php (added to /include) 
$body=file_get_html($yahooURL); 
$tables = $body->find('table'); 
$dom = new DOMDocument(); 
$elements[] = null; 
$dom->loadHtml($tables[1]); 
$x = new DOMXpath($dom); 
$i = 0; 
foreach($x->query('//td') as $td){ 
     $elements[$i] = $td -> textContent." "; 
    $i++; 
} 
$open = floatval($elements[1]); 
$high = floatval($elements[2]); 
$low = floatval($elements[3]); 
$close = floatval($elements[5]); 
$vol = str_replace(',', '', $elements[6]); 
$vol = floatval($vol); 
$date = date('Y-m-d'); 
$datestamp = strtotime($date); 
$date = date('Y-m-d',$datestamp); 
    // return stock as an associative array 
    return [ 
     "symbol" => $symbol, 
     "name" => $name, 
     "price" => $close, 
     "open" => $open, 
     "high" => $high, 
     "low" => $low, 
     "vol" => $vol, 
     "date" => $date 
    ]; 
}