-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetmeta.php
216 lines (131 loc) · 3.31 KB
/
getmeta.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
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
<?php
/**
* Script to allow for the gathering of external site meta data via ajax call.
* Not designed to be used via the browser.
*
*
*
* @file
* @author James Alexander
* @copyright © 2015 James Ryan Alexander
* @license MIT at http://opensource.org/licenses/MIT and LICENSE.txt which should be in the root folder of this software
* @version 1.0 - 2015-09-30
*/
date_default_timezone_set( 'UTC' );
$urlarray = null;
$url = null;
$action = null;
if ( isset( $_POST['action'] ) ) {
$action = $_POST['action'];
}
if ( isset( $_POST['url'] ) ) {
$url = $_POST['url'];
}
function getPageData( $url ) {
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
$result = curl_exec( $ch );
curl_close( $ch );
return $result;
}
function getMetaTags( $url ) {
$pagedata = getPageData( $url );
$page = new DomDocument();
libxml_use_internal_errors(true);
$page->loadHTML( $pagedata );
$metaDom = $page->getelementsbytagname( 'meta' );
return $metaDom;
}
function getTitle( $url ) {
$pagedata = getPageData( $url );
if ( $pagedata ) {
$page = new DomDocument();
libxml_use_internal_errors(true);
$page->loadHTML( $pagedata );
$titleDom = $page->getelementsbytagname( 'title' );
if ($titleDom->length > 0)
{
$title = $titleDom->item(0)->textContent;
}
// fall back if first tag it finds is null cause... apparently that's a thing occasionally from testing
if ( $title == null ) {
if ( $titleDom->length > 1 ) {
$title = $titleDom->item(1)->textContent;
}
}
return $title;
} else {
return 'no data recieved';
}
}
function getDescription ( $url, $type = null ) {
$metaTags = getMetaTags( $url );
$desc = null;
$ogdesc = null;
for ( $i = 0; $i < $metaTags->length; $i++ ) {
$metaTag = $metaTags->item($i);
if ( strtolower( $metaTag->getAttribute('name') ) == 'description' ) {
$desc = $metaTag->getAttribute('content');
}
if ( strtolower( $metaTag->getAttribute('property') ) == 'og:description' ) {
$ogdesc = $metaTag->getAttribute('content');
}
}
if ( $type = 'og' ) {
if ( $ogdesc ) {
return $ogdesc;
} else {
return 'no description found';
}
} else {
if ( $desc ) {
return $desc;
} else {
if ( $ogdesc ) {
return $ogdesc;
} else {
return 'no description found';
}
}
}
}
if ( $action ) {
switch ( $action ) {
case 'title':
if ( $url ) {
$result = getTitle( $url );
echo json_encode( $result );
} else {
echo json_encode( 'no url detected, either check your input or complain to maintainer' );
}
break;
case 'description':
if ( $url ) {
$result = getDescription( $url );
echo json_encode( $result );
} else {
echo json_encode( 'no url detected, either check your input or complain to maintainer' );
}
break;
case 'ogdescription':
if ( $url ) {
$result = getDescription( $url, 'og' );
echo json_encode( $result );
} else {
echo json_encode( 'no url detected, either check your input or complain to maintainer' );
}
break;
case 'multi':
default:
echo json_encode( "not title?" );
break;
}
}
/*
//For testing
else {
echo getTitle( "http://php.net/manual/en/class.domnodelist.php" );
}
*/