E
E
Evgeny Ivanov2017-11-06 14:44:02
PHP
Evgeny Ivanov, 2017-11-06 14:44:02

How to get information from finance.yahoo?

You need to receive information from finance.yahoo automatically.
There is a link
https://finance.yahoo.com/quote/AAPL/history?perio...
You need to get information from the table. Of course, you can parse html, but there is what you need nearby - a link to csv (Download Data). The problem is that there is no way I can get the correct link.
They say it's a matter of cookies - but I still don't understand where exactly and what else should be inserted into the link.
My code (working, but missing a parameter in the request or a cookie)

//Получить информацию для подстановки в запрос из url
function yahoo_get_crumb_from_url($url)
{
$crumb='';
$data=file_get_contents($url);
$pattern = '|CrumbStore":{"crumb":"(.+?)\"|is'; 
preg_match($pattern, $data, $out); 
return $out[1];
}

//Получить информацию о курсе акций для выбранного тикера и дат (например AAPL,1509649200,1509908400)
//Использует функцию yahoo_get_crumb_from_url
function yahoo_get_stock_information($ticker,$date_begin,$date_end)
{
$today=time();
//Получаем информацию для подстановки в запрос из url
$url='https://finance.yahoo.com/quote/AAPL/history?period1='.$today.'&period2='.$today.'&filter=history';
$crumb=yahoo_get_crumb_from_url($url); 
$url='https://query1.finance.yahoo.com/v7/finance/download/'.$ticker.'?period1='.$date_begin.'&period2='.$date_end.'&events=history&crumb='.$crumb;
$data=file_get_contents($url);
return $data;
}

//Вызов
echo yahoo_get_stock_information('AAPL',1509649200,1509908400);
//Результат - не авторизированный доступ. 
//HTTP request failed! HTTP/1.0 401 Unauthorized

And if we follow the generated link directly, for example, now the link is
https://query1.finance.yahoo.com/v7/finance/downlo...
we get
{
    "finance": {
        "error": {
            "code": "Unauthorized",
            "description": "Invalid cookie"
        }
    }
}

Tell me what else needs to be inserted into my url - what kind of cookie (insert as a parameter or set_cookie ) and where can I get it from?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2017-11-06
@proudmore

The file must be downloaded not through file_get_content, forget about it.
The CSV may be too large to fit into the allocated amount of RAM.
Download via curl. You can set a cookie on it. You will need to figure out which cookie leads to authorization and add it to the curl request.

E
Evgeny Ivanov, 2017-11-10
@logpol32

If someone else is interested - then scored on this table. As I understand it, there is data for the day, and they have real-time data. It's easier to parse and fetch data every 10-30 minutes.
I just don't know how yahoo will react to such requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question