/
searchengineAPIs.php
201 lines (163 loc) · 6.34 KB
/
searchengineAPIs.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
Problem definition: The lazy engineer Alex is tired of searching on multiple engines to get the best result.
Help Alex by making a program that given a search term goes into X amount of search engines, do the search and
then return a list of the result.
He wants to have two different ways to sort the results, and the ability to add more ways later without much problems.
Definition: (Given a engine count of 2) List of results ( [Response 0 for engine 0], [Response 0 for engine 1],
[Response 1 for engine 0]. [Response 1 for engine 1] ..... [Response n for engine 2] )
For the second algorithm he wants to introduce random weighting of the results, for example: 60% priority for engine 1
and 40% priority for engine 2. (this could for example mean that engine 1 got the first two results in the list).
Any search engine can be used (minimum 2) (Google and Bing have good and simple API's)
Focus should be spent on: Modularity, Extensibility and Readability.
*/
abstract class Search {
private $position;
private $reverse_position;
private $results;
abstract public function perform_search($search_query);
abstract public function return_next_result($reverse);
};
class GoogleSearch extends Search {
private $position = 0;
private $reverse_position = 0;
private $results = array();
# Perform the search
public function perform_search($search_query) {
$search_query_enc = urlencode($search_query);
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={$search_query_enc}";
$query_result = file_get_contents($url);
$json = json_decode($query_result);
$this->results = $json->responseData->results;
$reverse_position = count($this->results);
return $this->results;
}
# Get the next highest result from the results object
public function return_next_result($reverse) {
if ($reverse) {
if ($this->reverse_position > 0) {
$return_value = $this->results[$this->position];
$this->position -= 1;
return $return_value;
} else {
return false;
}
} else {
if (count($this->results) > $this->position) {
$return_value = $this->results[$this->position];
$this->position += 1;
return $return_value;
} else {
return false;
}
}
}
};
class BingSearch extends Search {
private $position = 0;
private $reverse_position = 0;
private $results = array();
# Perform the search
public function perform_search($search_query) {
$acctKey = '5uZKxv4C12/wIyYFY+K6iuDSUJsi0Gggvf3tE8rooto';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';
// Encode the query and the single quotes that must surround it.
$search_query_enc = urlencode("'{$search_query}'");
// Construct the full URI for the query.
$requestUri = "{$rootUri}/Web?\$format=json&Query={$search_query_enc}";
// Encode the credentials and create the stream context.
$auth = base64_encode("$acctKey:$acctKey");
$data = array(
'http' => array(
'request_fulluri' => true,
'ignore_errors' => true,
'header' => "Authorization: Basic $auth")
);
$context = stream_context_create($data);
$response = file_get_contents($requestUri, 0, $context);
$json = json_decode($response);
$this->results = $json->d->results;
$reverse_position = count($this->results);
return $this->results;
}
# Get the next highest result from the results object
public function return_next_result($reverse) {
if ($reverse) {
if ($this->reverse_position > 0) {
$return_value = $this->results[$this->position];
$this->position -= 1;
return $return_value;
} else {
return false;
}
} else {
if (count($this->results) > $this->position) {
$return_value = $this->results[$this->position];
$this->position += 1;
return $return_value;
} else {
return false;
}
}
}
};
/*
* Takes an array of search engines to use
* Only 'google' and 'bing' are implemented.
*/
function get_search_engine_results($search_query, $engine_array, $weight_array = null, $reverse = 0)
{
foreach ($engine_array as $engine_type) {
switch ($engine_type) {
case 'google':
$engine_object = new GoogleSearch();
break;
case 'bing':
$engine_object = new BingSearch();
break;
default:
return 0;
}
$res = $engine_object->perform_search($search_query);
$object_array[] = $engine_object;
}
# Construct the return array
$return_array = array();
$no_more_results = 0;
if ($weight_array) {
# Return results in a weighted fashion
while ($no_more_results == 0) {
$rand = mt_rand(1, (int)array_sum($weight_array));
foreach ($weight_array as $key => $value) {
$rand -= $value;
if ($rand <= 0) {
$result = $object_array[$key]->return_next_result($reverse);
break;
}
}
if ($result) {
$return_array[] = $result;
} else {
$no_more_results = 1;
}
}
} else {
# Return normal list of results ( [Response 0 for engine 0], [Response 0 for engine 1],
# [Response 1 for engine 0]. [Response 1 for engine 1] ..... [Response n for engine 2] )
while ($no_more_results == 0) {
foreach ($object_array as $search_object) {
$result = $search_object->return_next_result($reverse);
if ($result) {
$return_array[] = $result;
} else {
$no_more_results = 1;
}
}
}
}
return $return_array;
}
$engine_array = array('google', 'bing');
$weight_array = array(1, 4);
$result = get_search_engine_results('test', $engine_array, $weight_array);
print_r($result);