雅虎财经 API file_get_contents 429 请求过多

问题描述 投票:0回答:1

我正在 https://query1.finance.yahoo.com/v8/finance/chart/Ticker 使用雅虎财经的 API 和 php 的 file_get_contents 并收到“无法打开流:HTTP 请求失败!HTTP/1.0 429 太多”请求”,但是当我从服务器的命令行使用curl到相同的URL时,我得到了实际的响应!我确认这两个请求都来自同一个 IP。

我没有发送很多请求 - 我昨晚发送了大约 40 个 get 请求,然后今天的第一个请求失败了。我的问题是 - 有人见过同样类型的问题吗?我读到限制是每小时 100 个请求 - 我远远低于这个限制!

如有任何建议,我们将不胜感激。

php nginx curl
1个回答
0
投票

既然您注意到curl可以工作,请尝试在您的请求中添加用户代理以模仿curl。

<?php

$url = "https://query1.finance.yahoo.com/v8/finance/chart/nvda"; 

$options = [
    'http' => [
        'method' => "GET",
        'header' => "User-Agent: curl/7.68.0\r\n"
    ]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

if ($response !== false) {
    echo "Response: " . $response;
} else {
    echo "Request failed";
}

?>

我在 Rust 中遇到了同样的问题,并在请求中添加了一个类似的用户代理修复了它。您还可以尝试浏览器用户代理,例如

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

© www.soinside.com 2019 - 2024. All rights reserved.