-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathOpenGraph.php
More file actions
132 lines (112 loc) · 4.42 KB
/
OpenGraph.php
File metadata and controls
132 lines (112 loc) · 4.42 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace shweshi\OpenGraph;
use DOMDocument;
use shweshi\OpenGraph\Exceptions\FetchException;
class OpenGraph
{
public function fetch($url, $allMeta = null, $lang = null, $options = LIBXML_NOWARNING | LIBXML_NOERROR, $userAgent = 'Curl')
{
$html = $this->curl_get_contents($url, $lang, $userAgent);
/**
* parsing starts here:.
*/
$doc = new DOMDocument();
$libxml_previous_state = libxml_use_internal_errors(true);
$doc->loadHTML('<?xml encoding="utf-8" ?>'.$html, $options);
//catch possible errors due to empty or malformed HTML
if ($options > 0 && ($options & (LIBXML_NOWARNING | LIBXML_NOERROR)) == 0) {
Log::warning(libxml_get_errors());
}
libxml_clear_errors();
// restore previous state
libxml_use_internal_errors($libxml_previous_state);
$tags = $doc->getElementsByTagName('meta');
$metadata = [];
if ($allMeta && !empty($doc->getElementsByTagName('title'))) {
$title = $doc->getElementsByTagName('title');
$metadata['title'] = $title->length > 0 ? $title->item(0)->textContent : null;
}
foreach ($tags as $tag) {
$metaproperty = ($tag->hasAttribute('property')) ? $tag->getAttribute('property') : $tag->getAttribute('name');
if (!$allMeta && $metaproperty && strpos($tag->getAttribute('property'), 'og:') === 0) {
$key = strtr(substr($metaproperty, 3), '-', '_');
$value = $this->get_meta_value($tag);
}
if ($allMeta && $metaproperty) {
$key = (strpos($metaproperty, 'og:') === 0) ? strtr(substr($metaproperty, 3), '-', '_') : $metaproperty;
$value = $this->get_meta_value($tag);
}
if (!empty($key)) {
$metadata[$key] = $value;
}
/*
* Verify image url
*/
if (isset($metadata['image'])) {
$isValidImageUrl = $this->verify_image_url($metadata['image']);
if (!$isValidImageUrl) {
$metadata['image'] = '';
}
}
}
return $metadata;
}
protected function curl_get_contents($url, $lang, $userAgent)
{
$headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Cache-Control: no-cache',
'User-Agent: '.$userAgent,
];
if ($lang) {
array_push($headers, 'Accept-Language: '.$lang);
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_FAILONERROR => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => 'UTF-8',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => $headers,
]);
$response = curl_exec($curl);
if (curl_errno(/** @scrutinizer ignore-type */ $curl) !== 0) {
throw new FetchException(curl_error(/** @scrutinizer ignore-type */ $curl), curl_errno($curl), null, curl_getinfo(/** @scrutinizer ignore-type */ $curl));
}
curl_close(/** @scrutinizer ignore-type */ $curl);
return $response;
}
protected function verify_image_url($url)
{
$path = parse_url($url, PHP_URL_PATH);
$encoded_path = array_map('urlencode', explode('/', $path));
$url = str_replace($path, implode('/', $encoded_path), $url);
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
try {
$headers = get_headers($url);
return stripos($headers[0], '200 OK') ? true : false;
} catch (\Exception $e) {
return false;
}
}
protected function get_meta_value($tag)
{
if (!empty($tag->getAttribute('content'))) {
$value = $tag->getAttribute('content');
} elseif (!empty($tag->getAttribute('value'))) {
$value = $tag->getAttribute('value');
} else {
$value = '';
}
return $value;
}
}