-
Notifications
You must be signed in to change notification settings - Fork 3
/
keyword_checker.php
104 lines (83 loc) · 2.77 KB
/
keyword_checker.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
<?php
const VERSION = '1.0.1v';
const DEFAULT_SEARCH_URL = 'https://www.google.com/search?num=10&q=';
const DEFAULT_USERAGENT = '';
main();
function main()
{
$cmd_argv = $_SERVER['argv'] ?? null;
$keywords_raw = $_GET['keywords'] ?? $cmd_argv[1] ?? '';
$search_url = $_GET['search_url'] ?? $cmd_argv[2] ?? DEFAULT_SEARCH_URL;
$useragent = $_GET['useragent'] ?? $cmd_argv[3] ?? DEFAULT_USERAGENT;
$keywords = explode(',', $keywords_raw);
if ($keywords_raw === '') {
print_help();
}
$json_result = fetch_and_parse_rankings($keywords, $search_url, $useragent);
echo $json_result;
}
function fetch_and_parse_rankings($keywords, $search_url, $useragent)
{
$res = array_map(function ($k) use ($useragent, $search_url) {
$url = $search_url . rawurlencode($k);
$res = fetch($url, $useragent);
$positions = [];
if ($res[1] === null) {
$positions = parse_rankings($res[0]);
}
return '{'
. '"fetch_url": "' . $url . '",'
. '"keyword": "' . $k . '",'
. '"positions": ' . json_encode($positions, JSON_UNESCAPED_SLASHES) . ','
. '"error": "' . $res[1] . '"'
. '}';
}, $keywords);
return "[" . implode(",", $res) . "]";
}
function parse_rankings($html)
{
$dom = new DOMDocument;
@$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$links = $xpath->query("//a[.//h3 and starts-with(@href, '/url?q=')]");
$urls_raw = [];
foreach ($links as $link) {
$urls_raw[] = $link->getAttribute('href');
}
$urls = array_map(function ($e) {
preg_match('/^\/url\?q=([^&]+)&.+$/', $e, $match);
$url_decoded = rawurldecode(rawurldecode($match[1]));
return $url_decoded;
}, $urls_raw);
return $urls;
}
function fetch($url = "", $useragent = "")
{
$ch = curl_init("");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
if ($output === false) {
return [null, "can not fetch content from url: " . $url];
}
return [$output, null];
}
function print_help()
{
if (isset($_SERVER['argv'])) {
echo " version: \n " . VERSION;
echo "\n command example:\n";
echo ' php ' . $_SERVER['SCRIPT_FILENAME'] . ' "web design,online shopping"';
} else {
echo '<pre>';
echo " version: \n " . VERSION;
echo "\n url example:\n";
echo ' ' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?keywords=web+design,online+shopping';
echo '</pre>';
}
exit();
}