-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib_opengraph.php
55 lines (37 loc) · 975 Bytes
/
lib_opengraph.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
<?php
######################################################
function opengraph_parse_html(&$html){
# See also: https://github.com/scottmac/opengraph/
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$ok = $doc->loadHTML($html);
if (! $ok){
return array( 'ok' => 0, 'error' => 'Failed to parse HTML' );
}
$tags = $doc->getElementsByTagName('meta');
if (! $tags->length){
return array( 'ok' => 0, 'error' => 'No meta tags' );
}
$og = array();
foreach ($tags as $tag){
if (! $tag->hasAttribute('property')){
continue;
}
$prop = $tag->getAttribute("property");
if (! preg_match("/^og\:(.*)$/", $prop, $m)){
continue;
}
$k = $m[1];
$v = $tag->getAttribute("content");
$og[$k] = $v;
}
if (! count($og)){
return array( 'ok' => 0, 'error' => 'No opengraph tags' );
}
return array(
'ok' => 1,
'graph' => $og,
);
}
######################################################
?>