...
Create the array of data to send to the API:
Code Block |
---|
theme | Eclipse |
---|
language | php |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
|
$data = array(
"apikey" => "987zyx", // Use your API key here
"objectType" => "animals",
"objectAction" => "define",
); |
Encode the array into a JSON string:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme | Eclipse |
---|
linenumbers | true |
---|
|
$jsonData = json_encode($data) |
Post the JSON data to the RescueGroups.org API:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme | Eclipse |
---|
linenumbers | true |
---|
|
// create a new cURL resource
$ch = curl_init();
// set options, url, etc.
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_URL, "https://api.rescuegroups.org/http/json");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$results = curl_error($ch)
} else {
// close cURL resource, and free up system resources
curl_close($ch);
$results = $result;
}
|
Decode the JSON results back into a PHP array:
Code Block |
---|
theme | Eclipse |
---|
language | php |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
|
$resultsArray = json_decode($results); |
...
Print out the array so we can see the actual definition:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme |
---|
Eclipse | linenumbers | true |
---|
|
print_r($resultsArray); |
...
Create the array of data to send to the API:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme | Eclipse |
---|
linenumbers | true |
---|
|
$data = array(
"apikey" => "987zyx",
"objectType" => "animals",
"objectAction" => "publicSearch",
"search" => array (
"resultStart" => 0,
"resultLimit" => 20,
"resultSort" => "animalID",
"resultOrder" => "asc",
"calcFoundRows" => "Yes", "filters" => array(
array(
"fieldName" => "animalSpecies",
"operation" => "equals",
"criteria" => "dog",
),
array(
"fieldName" => "animalGeneralSizePotential",
"operation" => "equals",
"criteria" => "small",
),
),
"fields" => array("animalID","animalOrgID","animalName","animalBreed")
),
); |
Encode the array into a JSON string:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme | Eclipse |
---|
linenumbers | true |
---|
|
$jsonData = json_encode($data); |
Post the JSON data to the RescueGroups.org API:
Code Block |
---|
theme | Eclipse |
---|
language | php |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
|
// create a new cURL resource
$ch = curl_init();
// set options, url, etc.
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_URL, "https://api.rescuegroups.org/http/json");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$results = curl_error($ch);
} else {
// close cURL resource, and free up system resources
curl_close($ch);
$results = $result;
}
|
Decode the JSON results back into a PHP array:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme |
---|
Eclipse | linenumbers | true |
---|
|
$resultsArray = json_decode($results); |
...
Print out the array so we can see the actual definition:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme | Eclipse |
---|
linenumbers | true |
---|
|
print_r($resultsArray); |
The resulting PHP array will look something like:
Code Block |
---|
theme | Eclipse |
---|
language | php | theme | Eclipse |
---|
linenumbers | true |
---|
|
Array
(
[status] => ok
[messages] => Array
(
[generalMessages] => Array
(
)
[recordMessages] => Array
(
)
)
[foundRows] => 101
[data] => Array
(
[2685836] => Array
(
[animalID] => 2685836
[animalOrgID] => 1
[animalName] => Al
)
[2685840] => Array
(
[animalID] => 2685840
[animalOrgID] => 1
[animalName] => Alfonso
)
More records would follow here...
)
)
|