-
Notifications
You must be signed in to change notification settings - Fork 0
/
Suggestions.php
54 lines (50 loc) · 1.39 KB
/
Suggestions.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
<?php
/**
* Get a list of search suggestions from Google API.
*
* @package : Suggestions;
* @author : Saleh Bin Homud;
* @version : 1.0;
* @link : https://github.com/Saleh7;
*/
class Suggestions{
public $Lang;
public $Query;
function __construct($Lang,$Query){
$this->query = urlencode($Query);
$this->lang = $Lang;
$this->Request();
}
/**
*
*/
public function Request(){
$agent = "AAPP Application/1.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.4)";
$google = "http://suggestqueries.google.com/complete/search?output=toolbar&q=".$this->query."&hl=".$this->lang;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $google);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xmlInput = curl_exec($ch);
$thisxml = iconv('windows-1256', 'utf-8', $xmlInput); //windows1256(arabic) to utf-8
$Suggest = $this->Suggest($thisxml);
curl_close($ch);
}
/**
*
*/
public function Suggest($thisxml) {
$DOMDo = new DOMDocument();
$DOMDo->loadxml($thisxml);
$toplevel = $DOMDo->getElementsByTagName('toplevel');
$Suggest = $DOMDo->getElementsByTagName('suggestion');
$Data = array();
foreach ($Suggest as $Suggests) {
$Data[] = $Suggests->getAttribute('data');
}
return $Data;
}
}
?>