-
Notifications
You must be signed in to change notification settings - Fork 7
/
pullanimals.php
79 lines (70 loc) · 2.79 KB
/
pullanimals.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
<?php
enum Species: int
{
case Dogs = 1;
case Cats = 2;
case All = 0;
case NotDogOrCat = 1003;
}
define('WP_USE_THEMES', false);
require('../../../wp-blog-header.php');
if(!isset($_GET)) {
echo "Get not set";
} else {
if(isset($_GET['type'])) {
header('HTTP/1.1 200 OK');
if($_GET['type']=="dog") { #Pull all animals located in dog rooms
header('Content-type: application/json');
$room_list = get_option("dog_room_list");
echo_json($room_list, Species::Dog);
} else if($_GET['type']=="cat") { #Pull all animals located in cat rooms
header('Content-type: application/json');
$room_list = get_option("cat_room_list");
echo_json($room_list, Species::Cats);
} else if($_GET['type']=="other") { #Pull all animals located in small animal rooms
header('Content-type: application/json');
$room_list = get_option("other_room_list");
echo_json($room_list, Species::NotDogOrCat);
} else {
echo "Unknown type set";
}
} else {
echo "No type set";
}
}
function test_for_empty_object($object) {
if(count($object) == 0) {
return false;
} else {
return true;
}
}
function echo_json($room_list, $species) {
#Split the list into an array
$room_array = explode(',', $room_list);
#Initialize an empty array to store JSON data
$json_array = [];
foreach($room_array as $room_string) {
$room_string = trim($room_string);
$room_string = preg_replace('/\s+/', '%20', $room_string);
$xml_string = file_get_contents('http://ws.petango.com/webservices/wsadoption.asmx/AdoptableSearch?authkey=' . get_option("pp_auth_key") . '&speciesID=' . $species->value . '&sex=A&ageGroup=ALL&location=' . $room_string . '&site=&onHold=A&orderBy=ID&primaryBreed=All&secondaryBreed=All&specialNeeds=A&noDogs=A&noCats=A&noKids=A&stageid=');
$xml = simplexml_load_string($xml_string);
#Loop through each XmlNode
foreach ($xml->XmlNode as $node) {
#Skip nil nodes
if ($node->attributes('xsi', true)->nil == 'true') {
continue;
}
$data = [];
#Get child elements of adoptableSearch objects (the animals)
foreach ($node->adoptableSearch->children() as $child) {
$data[$child->getName()] = strval($child); #Convert SimpleXMLElement to string
}
#Append data to json_array array
$json_array[] = $data;
}
}
#encode and return the mega-object
echo json_encode($json_array);
}
?>