-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSearchResult.php
34 lines (26 loc) · 1.19 KB
/
getSearchResult.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
<?php
$accessKey = 'ASSESSKEY'; //suppose to be a valid assess key here.
$endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/search';
function BingWebSearch ($url, $key, $query) {
// Prepare HTTP request
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$headers = "Ocp-Apim-Subscription-Key: $key\r\n";
$options = array ('http' => array (
'header' => $headers,
'method' => 'GET'));
// Perform the Web request and get the JSON response
$context = stream_context_create($options);
$query = $query . " -site:wikipedia.org"; // filter out all wikipedia results
$result = file_get_contents($url . "?q=" . urlencode($query), false, $context);
// Extract Bing HTTP headers
$headers = array();
foreach ($http_response_header as $k => $v) {
$h = explode(":", $v, 2);
if (isset($h[1]))
if (preg_match("/^BingAPIs-/", $h[0]) || preg_match("/^X-MSEdge-/", $h[0]))
$headers[trim($h[0])] = trim($h[1]);
}
return array($headers, $result);
}
?>