-
Notifications
You must be signed in to change notification settings - Fork 17
/
whereIs.php
63 lines (50 loc) · 1.77 KB
/
whereIs.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
<?php
// whereIs.php
// Outputs a JSON formatted message { "location" : "LOCATION STRING" }
// based on a supplied GET paramater called 'name', eg: /whereIs.php?name=CattyMcCatface
// Set $silence_other_functions to true if you want to hide their print output.
$silence_other_functions = true;
include_once 'getPets.php';
print "{ \"location\": \"";
//Must be passed a GET parameter named 'name',
if(isset($_GET['name']) && !empty($_GET['name'])){
$supplied_pet_name = $_GET['name'];
$discovered_pet_id = -1;
// Need to find out if we have a pet by this name
foreach($pets as $currentpet) {
$sanitised_supplied_pet_name = strtolower($supplied_pet_name);
$sanitised_current_pet_name = strtolower($currentpet->pet_name);
//print "Checking supplied (" . $supplied_pet_name . ") against current (" . $currentpet->pet_name . ")\n";
if(strcmp($sanitised_current_pet_name,$sanitised_supplied_pet_name) == 0) {
//print "match found\n";
// This is the pet...
$discovered_pet_id = $currentpet->pet_id;
// Break out of the foreach loop... we've found our pet
break 1;
}
}
// Right then, have we got a pet?
if($discovered_pet_id >= 0) {
//Yup, so where is the little bugger?
$ch = curl_init($endpoint."/api/pet/$discovered_pet_id/position");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token"));
$result = json_decode(curl_exec($ch),true) or die("Curl Failed\n");
if($result['data']) {
if($result['data']['where']=="1") {
print "inside";
} else {
print "outside";
}
}
}
else {
//print "No pet with that name found";
print "not found";
}
} else {
//print "No pet name supplied.";
print "not found";
}
print "\" }";
?>