-
Notifications
You must be signed in to change notification settings - Fork 31
/
RankChecker.class.php
169 lines (149 loc) · 6.66 KB
/
RankChecker.class.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
<?php
if (@ini_set('max_execution_time', 1200) !== false) {
@ini_set('max_execution_time', 1200);
}
if (!class_exists('GoogleRankChecker')) {
class GoogleRankChecker
{
public $start;
public $end;
public function __construct($start = 1, $end = 2)
{
$this->start = $start;
$this->end = $end;
}
public function find($keyword, $useproxie, $proxies)
{
$results = [];
for ($start = ($this->start-1) * 10; $start <= $this->end * 10; $start += 10) {
$ua = [
0 => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201',
10 => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
20 => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
];
if ($useproxie) {
$host = $proxies['host'];
$port = $proxies['port'];
$username = $proxies['username'];
$password = $proxies['password'];
if (!empty($username)) {
$auth = base64_encode($username . ':' . $password);
$useauth = sprintf('Proxy-Authorization: Basic %s', $auth);
} else {
$useauth = '';
}
$options = [
'http' => [
'method' => 'GET',
'header' => "Accept-language: en\r\n" .
"Cookie: PHP Google Keyword Position\r\n" .
"User-Agent: " . $ua[$start] . "\r\n".
$useauth,
'proxy' => sprintf('tcp://%s:%s', $host, $port),
'request_fulluri' => true
]
];
} else {
$options = [
'http' => [
'method' => 'GET',
'header' => "Accept-language: en\r\n" .
"Cookie: PHP Google Keyword Position\r\n" .
"User-Agent: " . $ua[$start]
]
];
}
if ($useproxie) {
if (!empty($username)) {
$auth = base64_encode($username . ':' . $password);
$arrayproxies = [
CURLOPT_PROXY => $host,
CURLOPT_PROXYPORT => $port,
CURLOPT_PROXYUSERPWD => $auth
];
} else {
$arrayproxies = [
CURLOPT_PROXY => $host,
CURLOPT_PROXYPORT => $port
];
}
} else {
$arrayproxies = [];
}
$keyword = str_replace(' ', '+', trim($keyword));
$url = sprintf('https://www.google.com/search?ie=UTF-8&q=%s&start=%s&num=30', $keyword, $start);
$context = stream_context_create($options);
if ($this->_isCurlEnabled()) {
$data = $this->_curl($url, $useproxie, $arrayproxies);
} else {
$data = @file_get_contents($url, false, $context);
}
if (is_array($data)) {
$errmsg = $data['errmsg'];
$results = ['rank' => 'zero', 'url' => $errmsg];
} else {
if (strpos($data, 'To continue, please type the characters below') !== false || $data == false
|| strpos($data, "We're sorry") !== false ) {
$results = ['rank' => 'zero', 'url' => ''];
} else {
$j = -1;
$i = 1;
while (($j = stripos($data, '<cite class="_Rm">', $j+1)) !== false) {
$k = stripos($data, '</cite>', $j);
$link = strip_tags(substr($data, $j, $k-$j));
$rank = $i++;
$results[] = ['rank' => $rank, 'url' => $link];
}
}
}
$sleep = rand(20, 25);
sleep($sleep);
}
return $results;
}
private function _isCurlEnabled()
{
return function_exists('curl_version');
}
private function _curl($url, $useproxie, $arrayproxies)
{
try {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
if ($useproxie) {
if (!empty($arrayproxies)) {
foreach($arrayproxies as $param => $val) {
curl_setopt($ch, $param, $val);
}
}
}
$content = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if (!$errno) {
return $content;
} else {
return [
'errno' => $errno,
'errmsg'=> $error
];
}
} catch (Exception $e) {
return [
'errno' => $e->getCode(),
'errmsg' => $e->getMessage()
];
}
}
}
}