-
Notifications
You must be signed in to change notification settings - Fork 13
/
metadata.class.php
143 lines (119 loc) · 3.45 KB
/
metadata.class.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
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
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
Copyright 2013 Ben Southall
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Original can be found at https://github.com/baj84/metadata/
*/
class MetaData implements Iterator {
/**
* Holds all the meta tags we get from the url
*
*/
private $_values = array();
/**
* Fetches a URI and parses it for meta tags, returns
* false on error.
*
* @param $URI URI to page to parse for Open Graph data
* @return MetaData
*/
static public function fetch($URI) {
$curl = curl_init($URI);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$response = curl_exec($curl);
curl_close($curl);
if(!empty($response)) {
return self::_parse($response);
}
else {
return false;
}
}
/**
* Parses HTML and extracts meta tags
*
* @param $HTML HTML to parse
* @return MetaData
*/
static private function _parse($HTML) {
$page = new self();
$rawTags = array();
preg_match_all("|<meta[^>]+=\"([^\"]*)\"[^>]" . "+content=\"([^\"]*)\"[^>]+>|i", $HTML, $rawTags, PREG_PATTERN_ORDER);
if(!empty($rawTags)) {
$multiValueTags = array_unique(array_diff_assoc($rawTags[1], array_unique($rawTags[1])));
for($i=0; $i < sizeof($rawTags[1]); $i++) {
$hasMultiValues = false;
$tag = $rawTags[1][$i];
foreach($multiValueTags as $mTag) {
if($tag == $mTag)
$hasMultiValues = true;
}
if($hasMultiValues) {
$page->_values[$tag][] = $rawTags[2][$i];
}
else {
$page->_values[$tag] = $rawTags[2][$i];
}
}
}
if (empty($page->_values)) { return false; }
return $page;
}
/**
* Helper method to access attribute array directly
*/
public function tags() {
return $this->_values;
}
/**
* Helper method to access attributes directly
* Example:
* $metaData->title
*
* @param $key Key to fetch from the lookup
*/
public function __get($key) {
if (array_key_exists($key, $this->_values)) {
return $this->_values[$key];
}
}
/**
* Return all the keys found on the page
*
* @return array
*/
public function keys() {
return array_keys($this->_values);
}
/**
* Helper method to check an attribute exists
*
* @param $key
*/
public function __isset($key) {
return array_key_exists($key, $this->_values);
}
/**
* Iterator code
*/
private $_position = 0;
public function rewind() { reset($this->_values); $this->_position = 0; }
public function current() { return current($this->_values); }
public function key() { return key($this->_values); }
public function next() { next($this->_values); ++$this->_position; }
public function valid() { return $this->_position < sizeof($this->_values); }
}