-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
88 lines (72 loc) · 2.22 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
<?php
header('Content-type: application/json');
$query = @$_GET['query'];
$country = @$_GET['country'];
if (!$query || strlen($query) > 50)
die(json_encode([
'status' => false,
'result' => 'bad query'
]));
if (!$country || strlen($country) !== 2)
die(json_encode([
'status' => false,
'result' => 'bad country'
]));
$prefix = 'https://itunes.apple.com/search?';
$url = $prefix .
http_build_query([
'term' => $query,
'country' => $country,
'media' => 'software',
'limit' => 30,
'lang' => 'en_us',
'explicit' => 'Yes',
'entity' => 'software,iPadSoftware,macSoftware'
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch));
if (!$response || $response->resultCount === 0)
die(json_encode([
'status' => false,
'result' => 'no result'
]));
$result = [];
foreach ($response->results as $application) {
$result[] = [
'id' => $application->trackId,
'title' => $application->trackCensoredName,
'platform' => $application->kind === 'mac-software' ? 'Mac' : detectPlatform($application->supportedDevices),
'developer' => $application->artistName,
'genre' => $application->primaryGenreName, // or genres[] maybe?
'icon' => $application->artworkUrl512,
'price' => $application->formattedPrice
];
}
echo json_encode([
'status' => true,
'result' => $result
]);
function existsInArray(array $haystack, string $needle) {
// Is Convert array to string and check with regex faster?
foreach ($haystack as $element) {
if (strstr($element, $needle))
return true;
}
}
function detectPlatform(array $supportedDevices) {
$devices = [];
if (existsInArray($supportedDevices, 'iPhone'))
$devices[] = 'iPhone';
if (existsInArray($supportedDevices, 'iPad'))
$devices[] = 'iPad';
if (existsInArray($supportedDevices, 'Mac'))
$devices[] = 'Mac';
if (count($devices) === 3)
return 'Universal';
else if (count($devices) === 1)
return $devices[0];
else
return implode(' & ', $devices);
}