-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-google-index.php
76 lines (58 loc) · 1.63 KB
/
count-google-index.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
<?php
/*
* Really really simple function return response as json
* Ferri Sutanto
* 25 Apr 2015 12:01PM.
*/
if (! function_exists('responseJson')) {
function responseJson($data = [], $statusCode = '200')
{
http_response_code($statusCode);
header('Content-Type: application/json');
echo json_encode($data);
die;
}
}
/*
* Taken from http://www.binarytides.com/validate-domain-name-filter_var-function-php/
*/
function filter_var_domain($domain)
{
if (stripos($domain, 'http://') === 0) {
$domain = substr($domain, 7);
}
///Not even a single . this will eliminate things like abcd, since http://abcd is reported valid
if (! substr_count($domain, '.')) {
return false;
}
if (stripos($domain, 'www.') === 0) {
$domain = substr($domain, 4);
}
$again = 'http://'.$domain;
return filter_var($again, FILTER_VALIDATE_URL);
}
$url = isset($_GET['url']) ? $_GET['url'] : null;
if (! $url) {
return responseJson([
'error' => 'URL is required',
], 403);
}
if (! $url = filter_var_domain($url)) {
return responseJson([
'error' => 'URL is not valid',
], 403);
}
$googleUrl = 'https://www.google.com/search?hl=en&gbv=1&q=site:'.urlencode($url);
$get = @file_get_contents($googleUrl);
$pattern = '#<div class="sd" id="resultStats">About (.*?) results</div>#';
$count = 0;
if (preg_match($pattern, $get, $match)) {
$count = (int) str_replace(',', '', $match[1]);
}
return responseJson([
'data' => [
'count' => $count,
'url' => $url,
'google' => $googleUrl,
],
]);