This repository has been archived by the owner on May 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.php
89 lines (76 loc) · 2.57 KB
/
search.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
<?php
$url = 'https://developer.mozilla.org/en-US/';
$search_url = $url . 'search.json?q=';
$query = $argv[1];
$results = array();
function toxml($a = null, $format = 'array') {
global $results;
if ($format == 'json'):
$a = json_decode($a, TRUE);
endif;
if (is_null($a) && !empty($results)):
$a = $results;
elseif (is_null($a) && empty($results)):
return false;
endif;
$items = new SimpleXMLElement("<items></items>"); // Create new XML element
foreach ($a as $b): // Lop through each object in the array
$c = $items->addChild('item'); // Add a new 'item' element for each object
$c_keys = array_keys($b); // Grab all the keys for that item
foreach ($c_keys as $key): // For each of those keys
if ($key == 'uid'):
$c->addAttribute('uid', $b[$key]);
elseif ($key == 'arg'):
$c->addAttribute('arg', $b[$key]);
elseif ($key == 'valid'):
if ($b[$key] == 'yes' || $b[$key] == 'no'):
$c->addAttribute('valid', $b[$key]);
endif;
elseif ($key == 'autocomplete'):
$c->addAttribute('autocomplete', $b[$key]);
else:
$c->$key = $b[$key];
endif;
endforeach;
endforeach;
return $items->asXML(); // Return XML string representation of the array
}
$results[] = array(
'uid' => 'placeholder',
'title' => 'Go to the website',
'subtitle' => $url,
'arg' => $url,
'icon' => 'icon.png',
'valid' => 'yes'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $search_url . $query,
));
$output = curl_exec($curl);
curl_close($curl);
$data = json_decode($output);
$results = array();
if (!empty($data) && $data->count > 0) {
foreach ($data->documents as $d):
$results[] = array(
'uid' => $d->slug,
'title' => $d->title,
'subtitle' => strip_tags($d->excerpt),
'arg' => $d->url,
'icon' => 'icon.png',
'valid' => 'yes'
);
endforeach;
} else {
$results[] = array(
'uid' => 'placeholder',
'title' => 'No documents were found that matched "'.$query.'".',
'subtitle' => 'Click to see the results for yourself',
'arg' => $url . 'search?q=' . $query,
'icon' => 'icon.png',
'valid' => 'yes'
);
}
echo toxml();