Meta.php
6.53 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace App\Classes;
class Meta
{
private static $STATUS = array(
"initialized"=>0,
"success"=>1,
"fail"=>2
);
private $metaData;
private $url;
private $html;
private $status;
private $meta;
/**
* __construct
*
* Require the url of the webpage for meta data extraction
*
* @access public
* @param string $url
*/
public function __construct($url) {
$this->url = $url;
$this->metaData = new \StdClass;
$this->metaData->title = "";
$this->metaData->description = "";
$this->metaData->keywords = "";
$this->metaData->image = "";
$this->initialized();
}
/**
* parse
*
* Parse the meta data from the given url and populate data member $metaData
*
* @access public
* @return integer
*/
public function parse() {
// load HTML as DOMDocument object for parsing
$this->html = new \DOMDocument;
libxml_use_internal_errors(true);
$this->html->loadHTML(file_get_contents($this->url));
// php built-in get_meta_tags() only read those with name "title", "description" and so on
// so I wrote my own version supporting twitter:title, og:title, etc.
$this->meta = $this->my_get_meta_tags($this->url);
$this->success(); // assume successful
// possible to add more method such as getAuthor()
$this->getTitle();
$this->getDescription();
$this->getKeywords();
$this->getImage();
return $this->status;
}
/**
* finalize
*
* Export the meta data parsed
*
* @access public
* @return StdClass
*/
public function finalize() {
$tmp = new \StdClass;
$tmp->url = $this->url;
$tmp->title = $this->metaData->title;
$tmp->description = $this->metaData->description;
$tmp->image = $this->metaData->image;
$tmp->status = $this->status;
$tmp->keywords = $this->metaData->keywords;
return $tmp;
}
/**
* my_get_meta_tags
*
* Require the url to be parsed, read every meta tags found
*
* @access private
* @param string $url
* @return array
*/
private function my_get_meta_tags($url) {
$metatags = $this->html->getElementsByTagName("meta");
$tmeta = array();
for ($i=0; $i<$metatags->length; ++$i) {
$item = $metatags->item($i);
$name = $item->getAttribute('name');
if (empty($name)) {
// og meta tags, or twitter meta tags
$tmeta[$item->getAttribute('property')] = $item->getAttribute('content');
}
else {
// conventional meta tags
$tmeta[$name] = $item->getAttribute('content');
}
}
return $tmeta;
}
/**
* initizlized
*
* Set the state of the object to be initizlied
*
* @access private
*/
private function initialized() {
$this->status = self::$STATUS["initialized"];
}
/**
* success
*
* Set the state of the object to be successful
*
* @access private
*/
private function success() {
$this->status = self::$STATUS["success"];
}
/**
* fail
*
* Set the state of the object to be failed
*
* @access private
*/
private function fail() {
$this->status = self::$STATUS["fail"];
}
/**
* getTitle
*
* Read meta title based on priorities of the tag name/property,
* fallback to reading <title> and <h1> if meta title not present
*
* @access private
*/
private function getTitle() {
if (isset($this->meta["og:title"])) {
$this->metaData->title = $this->meta["og:title"];
return;
}
if (isset($this->meta["twitter:title"])) {
$this->metaData->title = $this->meta["twitter:title"];
return;
}
if (isset($this->meta["title"])) {
$this->metaData->title = $this->meta["title"];
return;
}
$title = $this->html->getElementsByTagName("title") or $title = $this->html->getElementsByTagName("h1");
// taking either the title or h1 tag
if (!$title->length) {
// if no h1 tag, nothing good enough to be the site title
$this->fail();
return;
}
else {
$this->metaData->title = ($title->length) ? $title->item(0)->nodeValue : "";
}
}
/**
* getDescription
*
* Read meta description based on priorities of the tag name/property.
* No fallback, it doesn't read anything except for the meta tag
*
* @access private
*/
private function getDescription() {
if (isset($this->meta["og:description"])) {
$this->metaData->description = $this->meta["og:description"];
return;
}
if (isset($this->meta["twitter:description"])) {
$this->metaData->description = $this->meta["twitter:description"];
return;
}
if (isset($this->meta["description"])) {
$this->metaData->description = $this->meta["description"];
return;
}
$this->fail();
return;
}
private function getKeywords() {
if (isset($this->meta["og:keywords"])) {
$this->metaData->keywords = $this->meta["og:keywords"];
return;
}
if (isset($this->meta["twitter:keywords"])) {
$this->metaData->keywords = $this->meta["twitter:description"];
return;
}
if (isset($this->meta["keywords"])) {
$this->metaData->keywords = $this->meta["keywords"];
return;
}
$this->fail();
return;
}
/**
* getImage
*
* Read meta image url based on priorities of the tag name/property.
* No fallback, it doesn't read anything except for the meta tag
*
* @access private
*/
private function getImage() {
if (isset($this->meta["og:image"])) {
$this->metaData->image = $this->meta["og:image"];
return;
}
if (isset($this->meta["twitter:image"])) {
$this->metaData->image = $this->meta["twitter:image"];
return;
}
if (isset($this->meta["image"])) {
$this->metaData->image = $this->meta["image"];
return;
}
$this->fail();
}
}