public function FacebookFetcher::getOembedData in Media entity facebook 8
Same name and namespace in other branches
- 8.2 src/FacebookFetcher.php \Drupal\media_entity_facebook\FacebookFetcher::getOembedData()
- 3.x src/FacebookFetcher.php \Drupal\media_entity_facebook\FacebookFetcher::getOembedData()
Fetch and return response from Facebook's oEmbed API endpoint.
Parameters
string $resource_url: The URL to pass to Facebook's oembed API.
File
- src/
FacebookFetcher.php, line 54
Class
- FacebookFetcher
- Class FacebookFetcher.
Namespace
Drupal\media_entity_facebookCode
public function getOembedData($resource_url) {
// Keep an in-memory cache of the response data for each URL, since this
// data may be requested multiple times on the same request.
static $memory_cache;
if (!isset($memory_cache)) {
$memory_cache = [];
}
if (!isset($memory_cache[$resource_url])) {
// If there was an error interacting with the Facebook API, like a network
// timeout due to Facebook being down, we don't want to clog up the Drupal
// site's resources by making lots of API requests that may all timeout.
// To do this, we mark when a request exception occurred and back out of
// subsequent requests if so.
if ($this->apiErrorEncountered) {
$this->loggerChannel
->error('Aborting Facebook API request due to previously encountered error.');
return FALSE;
}
$endpoint = $this
->getApiEndpointUrl($resource_url) . '?url=' . $resource_url;
try {
$response = $this->httpClient
->request('GET', $endpoint, [
'timeout' => 5,
]);
$decoded = json_decode((string) $response
->getBody(), TRUE);
$memory_cache[$resource_url] = $decoded;
} catch (TransferException $e) {
\Drupal::logger('media_entity_facebook')
->error('Error retrieving oEmbed data for a Facebook media entity: @error', [
'@error' => $e
->getMessage(),
]);
$this->apiErrorEncountered = TRUE;
return FALSE;
}
}
return $memory_cache[$resource_url];
}