httpClient = $client; $this->loggerFactory = $loggerFactory; $this->cache = $cache; } /** * {@inheritdoc} */ public function fetchInstagramEmbed($shortcode, $hidecaption = FALSE, $maxWidth = NULL) { $options = [ 'url' => self::INSTAGRAM_URL . $shortcode . '/', 'hidecaption' => (int) $hidecaption, 'omitscript' => 1, ]; if ($maxWidth) { $options['maxwidth'] = $maxWidth; } // Tweets don't change much, so pull it out of the cache (if we have one) // if this one has already been fetched. $cacheKey = md5(serialize($options)); if ($this->cache && $cached_instagram_post = $this->cache->get($cacheKey)) { return $cached_instagram_post->data; } $queryParameter = UrlHelper::buildQuery($options); try { $response = $this->httpClient->request( 'GET', self::INSTAGRAM_API . '?' . $queryParameter, ['timeout' => 5] ); if ($response->getStatusCode() === 200) { $data = Json::decode($response->getBody()->getContents()); } } catch (RequestException $e) { $this->loggerFactory->get('media_entity_instagram')->error("Could not retrieve Instagram post $shortcode.", Error::decodeException($e)); } // If we got data from Instagram oEmbed request, return data. if (isset($data)) { // If we have a cache, store the response for future use. if ($this->cache) { // Instagram posts don't change often, so the response should expire // from the cache on its own in 90 days. $this->cache->set($cacheKey, $data, time() + (86400 * 90)); } return $data; } return FALSE; } }