|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Util; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* HTML utility class. |
|
20
|
|
|
* |
|
21
|
|
|
* This class provides utility methods for HTML manipulation. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Html |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Extract Open Graph meta tags from HTML content. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $html The HTML content to parse |
|
29
|
|
|
* |
|
30
|
|
|
* @return array An associative array of Open Graph meta tags |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function getOpenGraphMetaTags(string $html): array |
|
33
|
|
|
{ |
|
34
|
|
|
$crawler = new Crawler(); |
|
35
|
|
|
$crawler->addHtmlContent($html); |
|
36
|
|
|
$metaTags = $crawler->filterXPath('//meta[starts-with(@property, "og:")]'); |
|
37
|
|
|
|
|
38
|
|
|
$ogTags = []; |
|
39
|
|
|
/** @var \DOMElement $metaTag */ |
|
40
|
|
|
foreach ($metaTags as $metaTag) { |
|
41
|
|
|
$property = $metaTag->getAttribute('property'); |
|
42
|
|
|
$content = $metaTag->getAttribute('content'); |
|
43
|
|
|
$ogTags[$property] = $content; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $ogTags; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Extract Twitter meta tags from HTML content. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $html The HTML content to parse |
|
53
|
|
|
* |
|
54
|
|
|
* @return array An associative array of Twitter meta tags |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getTwitterMetaTags(string $html): array |
|
57
|
|
|
{ |
|
58
|
|
|
$crawler = new Crawler(); |
|
59
|
|
|
$crawler->addHtmlContent($html); |
|
60
|
|
|
$metaTags = $crawler->filterXPath('//meta[starts-with(@name, "twitter:")]'); |
|
61
|
|
|
|
|
62
|
|
|
$twitterTags = []; |
|
63
|
|
|
/** @var \DOMElement $metaTag */ |
|
64
|
|
|
foreach ($metaTags as $metaTag) { |
|
65
|
|
|
$name = $metaTag->getAttribute('name'); |
|
66
|
|
|
$content = $metaTag->getAttribute('content'); |
|
67
|
|
|
$twitterTags[$name] = $content; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $twitterTags; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the image URL from Open Graph or Twitter meta tags. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $html The HTML content to parse |
|
77
|
|
|
* |
|
78
|
|
|
* @return string|null The image URL if found, null otherwise |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function getImageFromMetaTags(string $html): ?string |
|
81
|
|
|
{ |
|
82
|
|
|
$ogTags = self::getOpenGraphMetaTags($html); |
|
83
|
|
|
if (isset($ogTags['og:image'])) { |
|
84
|
|
|
return $ogTags['og:image']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$twitterTags = self::getTwitterMetaTags($html); |
|
88
|
|
|
if (isset($twitterTags['twitter:image'])) { |
|
89
|
|
|
return $twitterTags['twitter:image']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|