-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing_search_api.php
145 lines (130 loc) · 4.17 KB
/
bing_search_api.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
<?php
/****
* Simple PHP application for using the Bing Search API
*/
$acctKey = '';
$rootUri = 'https://api.datamarket.azure.com/Bing/Search';
// Read the contents of the .html file into a string.
$contents = file_get_contents('bing_search_api.html');
if ($_POST['query'])
{
// Encode the query and the single quotes that must surround it.
$querybuilder = $_POST['query'];
// Set site target.
if ($_POST['Site'] !== '')
{
$querybuilder .= " site:{$_POST['Site']}";
}
// Encode the query and the single quotes that must surround it.
$query = urlencode("'{$querybuilder}'");
// Get the selected service operation (Web or Image).
$serviceOp = $_POST['service_op'];
// Construct the full URI for the query.
$requestUri = "$rootUri/$serviceOp?Query=$query";
if ($_POST['Top'] !== '')
{
$requestUri .= "&\$top={$_POST['Top']}";
}
if ($_POST['Skip'] !== '')
{
$requestUri .= "&\$skip={$_POST['Skip']}";
}
//if ($_POST['Format'] == 'JSON')
//{
$requestUri .= "&\$format=json";
//}
switch ($_POST['Adult'])
{
case 'Off':
$requestUri .= '&Adult=%27Off%27';
break;
case 'Moderate':
$requestUri .= '&Adult=%27Moderate%27';
break;
case 'Strict':
$requestUri .= '&Adult=%27Strict%27';
break;
}
if ($_POST['Market'] !== '')
{
$requestUri .= "&Market=%27{$_POST['Market']}%27";
}
if ($_POST['Latitude'] !== '')
{
$requestUri .= "&Latitude=%27{$_POST['Latitude']}%27";
}
if ($_POST['Longitude'] !== '')
{
$requestUri .= "&Longitude=%27{$_POST['Longitude']}%27";
}
if (isset($_POST['Options_DisableLocationDetection']) && isset($_POST['Options_EnableHighlighting']))
{
$requestUri .= '&Options=%27DisableLocationDetection%2BEnableHighlighting%27';
}
else if (isset($_POST['Options_EnableHighlighting']))
{
$requestUri .= '&Options=%27EnableHighlighting%27';
}
else if (isset($_POST['Options_DisableLocationDetection']))
{
$requestUri .= '&Options=%27DisableLocationDetection%27';
}
if (isset($_POST['WebSearchOptions_DisableHostCollapsing']) && isset($_POST['WebSearchOptions_DisableQueryAlterations']))
{
$requestUri .= '&WebSearchOptions=%27DisableQueryAlterations%2BDisableHostCollapsing%27';
}
else if (isset($_POST['WebSearchOptions_DisableHostCollapsing']))
{
$requestUri .= '&WebSearchOptions=%27DisableHostCollapsing%27';
}
else if (isset($_POST['WebSearchOptions_DisableQueryAlterations']))
{
$requestUri .= '&WebSearchOptions=%27DisableQueryAlterations%27';
}
// Encode the credentials and create the stream context.
$auth = base64_encode("$acctKey:$acctKey");
$data = array(
'http' => array(
'request_fulluri' => true,
// ignore_errors can help debug – remove for production. This option added in PHP 5.2.10
'ignore_errors' => true,
'header' => "Authorization: Basic $auth")
);
$context = stream_context_create($data);
// Get the response from Bing.
$response = file_get_contents($requestUri, 0, $context);
$resultStr = "<p>URI: $requestUri</p>";
// Format search results
if (isset($_POST['format_results']))
{
// Decode the response.
$jsonObj = json_decode($response);
// Parse each result according to its metadata type.
foreach ($jsonObj->d->results as $value)
{
switch ($value->__metadata->type)
{
case 'WebResult':
$resultStr .=
//"<p><a href=\"{$value->Url}\">{$value->Title}</a><br />{$value->Description}</p>";
"<p><b>{$value->Title}</b><br /><a href=\"{$value->Url}\" target=\"_blank\">{$value->DisplayUrl}</a><br />{$value->Description}</p>";
break;
case 'ImageResult':
$resultStr .=
"<h4>{$value->Title} ({$value->Width}x{$value->Height}) " .
"({$value->FileSize} bytes)</h4>" .
"<a href=\"{$value->MediaUrl}\">" .
"<img src=\"{$value->Thumbnail->MediaUrl}\"></a><br />";
break;
}
}
}
else // Just print out the results
{
$resultStr .= $response;
}
// Substitute the results placeholder. Ready to go.
$contents = str_replace('{RESULTS}', $resultStr, $contents);
}
echo $contents;
?>