-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkOgData.php
executable file
·98 lines (81 loc) · 3.01 KB
/
LinkOgData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace Statamic\Addons\LinkOgData;
use Statamic\Extend\Extensible;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Symfony\Component\DomCrawler\Crawler;
class LinkOgData
{
public function getOgData($url) {
// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);
// Are we looking at a url on this server?
if (preg_match("/^\//", $url))
{
$url = $_SERVER['HTTP_HOST'] . $url;
}
// // Make sure we have a protocol
if (!preg_match("/^http(s)?:\/\//", $url))
{
$url = 'http://' . $url;
}
if (!filter_var($url, FILTER_VALIDATE_URL))
{
return [];
}
if (!isset(parse_url($url)['host']) ||
(!checkdnsrr(parse_url($url)['host'], 'A') &&
!checkdnsrr(parse_url($url)['host'], 'CNAME')))
{
return [];
}
// create http client instance
$client = new Client(['http_errors' => false]);
// Make an async request
$request = new Request('GET', $url);
$result = [
'og' => [],
'twitter' => [],
];
try {
$response = $client->send($request);
} catch (\Exception $ex) {
return [];
}
if ($response->getStatusCode() != 200)
{
return [];
}
$crawler = new Crawler(mb_convert_encoding($response->getBody()->getContents(), 'HTML-ENTITIES', "UTF-8"));
// Title
$result['title'] = ($crawler->filter('title')->count() > 0) ? $crawler->filter('title')->text() : '';
// Other meta tags including description, keywords and author
$meta = $crawler->filter('meta[name="description"], meta[name="keywords"], meta[name="author"]');
foreach ($meta as $i => $tag) {
$result[$tag->getAttribute('name')] = $tag->getAttribute('content');
}
// OG Tags
$og_tags = $crawler->filter('meta[property^="og"]');
foreach ($og_tags as $i => $tag) {
// extract the values needed
// get the name of the property, removing `og:` and replacing any remaining `:` with `_`
$property = preg_replace('/:/', '_', preg_replace('/^og:/', '', $tag->getAttribute('property')));
// get the value of the property
$value = $tag->getAttribute('content');
// add the property to the array
$result['og'][$property] = $value;
}
// Twitter Tags
$twitter_tags = $crawler->filter('meta[name^="twitter"]');
foreach ($twitter_tags as $i => $tag) {
// extract the values needed
// get the name of the property, removing `og:` and replacing any remaining `:` with `_`
$property = preg_replace('/:/', '_', preg_replace('/^twitter:/', '', $tag->getAttribute('name')));
// get the value of the property
$value = $tag->getAttribute('content');
// add the property to the array
$result['twitter'][$property] = $value;
}
return $result;
}
}