-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaGeter.php
83 lines (72 loc) · 2.61 KB
/
MetaGeter.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
<?php
/**
*
* MetaGeter
* > get any metatag from site Director or Content HTML
* @param meta <metatage name Like <title|description|keywords|article:title|article:auther| etc .... >
* @param WS_URL site URL
* @param WSContentHTML HTML Site Content
*
* @return mixed
*/
function MetaGeter($meta = 'title', $WS_URL = null, $WSContentHTML = null)
{
// start handle HTML Content
if ( $WS_URL === null && $WSContentHTML === null)
return 'where is site content my Hero ?! ';
if ( $WSContentHTML === null && null != $WS_URL)
$WSContentHTML = getContentFromURL($WS_URL);
// ./ end HTML
// start Xpath
$document = new \DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true); // set error level
$document->loadHTML($WSContentHTML);
$xpath = new \DOMXPath($document);
// get data
switch( $meta )
{
case 'title' :
$Matched = $document->getElementsByTagName('title');
break;
default:
if ( strpos($meta, 'og:') !== false ){
$Matched = $xpath->query("//meta[@property='".$meta."']/@content");
}else{
$Matched = $xpath->query("//meta[@name='".$meta."']/@content");
}
break;
}
if ( is_object($Matched->item(0)) )
return utf8_decode($Matched->item(0)->nodeValue);
else
return null;
}
/**
* getContentFromURL
* > get site content via CURL
* @param URL
* @param referer
* @return mixed
*/
function getContentFromURL ($url, $referer = '')
{
if ( ! empty($url) )
{
$parse = parse_url($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
if(!empty($referer))
curl_setopt ($ch, CURLOPT_REFERER, $referer);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode != 200)
return false;
curl_close($ch);
return ($data);
}
}
?>