diff --git a/src/ApiClient.php b/src/ApiClient.php index 0f09160..1005184 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -34,10 +34,23 @@ class ApiClient */ private $resultDecoder; + /** + * @var string + */ + private $userAgent; + public function __construct(ClientInterface $guzzleClient, ResultDecoder $resultDecoder) { $this->client = $guzzleClient; $this->resultDecoder = $resultDecoder; + $this->userAgent = UserAgent::getRandomUserAgent(); + } + + public function getHeaders(): array + { + return [ + 'User-Agent' => $this->userAgent, + ]; } /** @@ -56,7 +69,7 @@ public function search(string $searchTerm, string $locale = 'en-US'): array .'?bkt=[%22findd-ctrl%22,%22fin-strm-test1%22,%22fndmtest%22,%22finnossl%22]&device=desktop&feature=canvassOffnet,finGrayNav,newContentAttribution,relatedVideoFeature,videoNativePlaylist,livecoverage&intl=us&lang=' .urlencode($locale) .'&partner=none&prid=eo2okrhcni00f®ion=DE&site=finance&tz=UTC&ver=0.102.432&returnMeta=true'; - $responseBody = (string) $this->client->request('GET', $url)->getBody(); + $responseBody = (string) $this->client->request('GET', $url, ['headers' => $this->getHeaders()])->getBody(); return $this->resultDecoder->transformSearchResult($responseBody); } @@ -196,15 +209,15 @@ private function fetchQuotes(array $symbols) // Initialize session cookies $initialUrl = 'https://fc.yahoo.com'; - $this->client->request('GET', $initialUrl, ['cookies' => $cookieJar, 'http_errors' => false]); + $this->client->request('GET', $initialUrl, ['cookies' => $cookieJar, 'http_errors' => false, 'headers' => $this->getHeaders()]); // Get crumb value $initialUrl = 'https://query'.$qs.'.finance.yahoo.com/v1/test/getcrumb'; - $crumb = (string) $this->client->request('GET', $initialUrl, ['cookies' => $cookieJar])->getBody(); + $crumb = (string) $this->client->request('GET', $initialUrl, ['cookies' => $cookieJar, 'headers' => $this->getHeaders()])->getBody(); // Fetch quotes $url = 'https://query'.$qs.'.finance.yahoo.com/v7/finance/quote?crumb='.$crumb.'&symbols='.urlencode(implode(',', $symbols)); - $responseBody = (string) $this->client->request('GET', $url, ['cookies' => $cookieJar])->getBody(); + $responseBody = (string) $this->client->request('GET', $url, ['cookies' => $cookieJar, 'headers' => $this->getHeaders()])->getBody(); return $this->resultDecoder->transformQuotes($responseBody); } @@ -214,7 +227,7 @@ private function getHistoricalDataResponseBody(string $symbol, string $interval, $qs = $this->getRandomQueryServer(); $dataUrl = 'https://query'.$qs.'.finance.yahoo.com/v7/finance/download/'.urlencode($symbol).'?period1='.$startDate->getTimestamp().'&period2='.$endDate->getTimestamp().'&interval='.$interval.'&events='.$filter; - return (string) $this->client->request('GET', $dataUrl)->getBody(); + return (string) $this->client->request('GET', $dataUrl, ['headers' => $this->getHeaders()])->getBody(); } private function validateIntervals(string $interval): void diff --git a/src/UserAgent.php b/src/UserAgent.php new file mode 100644 index 0000000..5acba0c --- /dev/null +++ b/src/UserAgent.php @@ -0,0 +1,87 @@ +