index-main/site/snippets/card-open-graph.php
Julie Blanc dc223ae1b8
All checks were successful
Deploy / Deploy to Production (push) Successful in 10s
card open-graph
2026-01-06 15:43:31 +01:00

108 lines
No EOL
5.4 KiB
PHP

<?php
// URL à scraper (remplacez par l'URL souhaitée)
$url = 'https://www.lemonde.fr/societe/article/2025/07/16/au-c-ur-des-emeutes-de-nouvelle-caledonie-quand-la-mort-de-banane-abattu-par-le-gign-lance-le-siege-de-saint-louis_6621496_3224.html';
// Fonction pour récupérer les données Open Graph
function getOpenGraphData($url) {
$ogData = [
'title' => '',
'description' => '',
'image' => '',
'site_name' => '',
'url' => $url
];
// Configuration du contexte pour éviter les erreurs SSL
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'timeout' => 10
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]);
// Récupérer le HTML
$html = @file_get_contents($url, false, $context);
if ($html === false) {
return $ogData;
}
// Parser les meta tags Open Graph
preg_match_all('/<meta\s+property=["\']og:([^"\']+)["\']\s+content=["\']([^"\']+)["\']/i', $html, $matches);
if (!empty($matches[1])) {
foreach ($matches[1] as $index => $property) {
$content = $matches[2][$index];
switch ($property) {
case 'title':
$ogData['title'] = htmlspecialchars($content);
break;
case 'description':
$ogData['description'] = htmlspecialchars($content);
break;
case 'image':
$ogData['image'] = $content;
break;
case 'site_name':
$ogData['site_name'] = htmlspecialchars($content);
break;
}
}
}
// Fallback: si pas de og:title, utiliser <title>
if (empty($ogData['title'])) {
preg_match('/<title>([^<]+)<\/title>/i', $html, $titleMatch);
if (!empty($titleMatch[1])) {
$ogData['title'] = htmlspecialchars($titleMatch[1]);
}
}
// Fallback: si pas de site_name, utiliser le domaine
if (empty($ogData['site_name'])) {
$parsed = parse_url($url);
$ogData['site_name'] = $parsed['host'] ?? '';
}
return $ogData;
}
$ogData = getOpenGraphData($url);
// Toujours utiliser le domaine de l'URL pour site_name
$parsed = parse_url($url);
$ogData['site_name'] = $parsed['host'] ?? '';
?>
<div class="card--open-graph">
<?php if (!empty($ogData['image'])): ?>
<figure>
<img src="<?= $ogData['image'] ?>" alt="<?= $ogData['title'] ?>">
</figure>
<?php endif; ?>
<div class="content">
<?php if (!empty($ogData['site_name'])): ?>
<span class="site-name"><?= $ogData['site_name'] ?></span>
<?php endif; ?>
<?php if (!empty($ogData['title'])): ?>
<h3 class="title">
<a href="<?= $ogData['url'] ?>" target="_blank">
<?= $ogData['title'] ?>
</a>
</h3>
<?php endif; ?>
<?php if (!empty($ogData['description'])): ?>
<p class="description"><?= $ogData['description'] ?></p>
<?php endif; ?>
</div>
<a class="link-block" href="<?= $ogData['url'] ?>" target="_blank"></a>
</div>