-
Notifications
You must be signed in to change notification settings - Fork 1
/
suggest.php
72 lines (52 loc) · 1.71 KB
/
suggest.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
<?php
class Suggest {
var $query;
var $locale;
var $xml;
var $data = array();
var $cache_path;
var $cache_file;
function Suggest($query, $locale) {
$this->query = urlencode($query);
$this->locale = $locale;
$this->cache_path = dirname(__FILE__) . '/Cache/';
$this->cache_file = $this->locale . "." . preg_replace("/[^a-z0-9.]+/i", "+", $this->query) . '.json';
if (file_exists($this->cache_path.$this->cache_file)) {
$cache = file_get_contents($this->cache_path.$this->cache_file);
$this->data = json_decode($cache);
} else {
$this->Query();
}
}
function Query(){
$agent = "AAPP Application/1.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.4)";
$host = "https://suggestqueries.google.com/complete/search?output=toolbar&hl=".$this->locale."&q=".$this->query;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$this->xml = curl_exec($ch);
curl_close($ch);
$this->Parse();
}
function Parse() {
$dom = new DOMDocument();
$dom->loadxml($this->xml);
$toplevel = $dom->getElementsByTagName('toplevel');
$suggestions = $dom->getElementsByTagName('suggestion');
foreach ($suggestions as $suggestion) {
$this->data[] = $suggestion->getAttribute('data');
}
$this->link = 'https://www.google.com/#safe=off&q='.$this->query.'&lr=lang_'.$this->locale;
$this->Cache();
}
function Cache() {
$json = json_encode($this->data);
if (is_writable($this->cache_path)) {
file_put_contents($this->cache_path.$this->cache_file, $json);
}
}
}
?>