« Return to Index
Sample Code
Example API implementations in PHP.
Fetch Questions
<?php
/**
*
* This example demonstrates using the API to request all job_type/question_category/question records.
*
*/
// build API request
$apiRequest = array(
"username" => "user@example.com",
"password" => "bWy1P2xRdyczAzmfxmUYpPjPc4Vg1",
"actions" => array(
array("name" => "search", "params" => array("entity" => "job_type")),
array("name" => "search", "params" => array("entity" => "question_category")),
array("name" => "search", "params" => array("entity" => "question"))
)
);
$apiRequestData = json_encode($apiRequest);
// send using cURL
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, "https://admin.inspectionapps.com/services/api");
curl_setopt($curlHandle, CURLOPT_POST, true); // set HTTP request method to POST
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // tell cURL to return response data, instead of sending to stdout
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $apiRequestData); // set post data
$apiResponseData = curl_exec($curlHandle);
curl_close($curlHandle);
// decode JSON
$apiResponse = @json_decode($apiResponseData);
if ($apiResponse == null)
die("Error processing server response: " . $apiResponseData);
// check for error
if (!$apiResponse->success)
die('Error on server: ' . $apiResponse->error);
// output results
$jobTypes = $apiResponse->results[0];
$categories = $apiResponse->results[1];
$questions = $apiResponse->results[2];
print "<ul>";
foreach ($jobTypes as $jobType) {
print "<li>" . htmlspecialchars($jobType->name) . "</li>";
print "<ul>";
foreach ($categories as $category) {
if ($category->idjob_type != $jobType->idjob_type)
continue;
print "<li>" . htmlspecialchars($category->name) . "</li>";
print "<ul>";
foreach ($questions as $question) {
if ($question->idquestion_category != $category->idquestion_category)
continue;
print "<li>" . htmlspecialchars($question->question) . "</li>";
}
print "</ul>";
}
print "</ul>";
}
print "</ul>";
Fetch a Specific Inspection
<?php
/**
*
* This example demonstrates using the API to request a specific inspection.
*
*/
// build API request
$apiRequest = array(
"username" => "user@example.com",
"password" => "bWy1P2xRdyczAzmfxmUYpPjPc4Vg1",
"actions" => array(
array("name" => "fetch", "params" => array("entity" => "inspection", "id" => "24"))
)
);
$apiRequestData = json_encode($apiRequest);
// send using cURL
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, "https://admin.inspectionapps.com/services/api");
curl_setopt($curlHandle, CURLOPT_POST, true); // set HTTP request method to POST
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // tell cURL to return response data, instead of sending to stdout
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $apiRequestData); // set post data
$apiResponseData = curl_exec($curlHandle);
curl_close($curlHandle);
// decode JSON
$apiResponse = @json_decode($apiResponseData);
if ($apiResponse == null)
die("Error processing server response: " . $apiResponseData);
// check for error
if (!$apiResponse->success)
die('Error on server: ' . $apiResponse->error);
// output results
$inspection = $apiResponse->results[0];
if (!$inspection)
die("server returned null");
print "<h1>Inspection #$inspection->idinspection</h1>";
print "<table border=1 cellspacing=0 cellpadding=4>";
foreach ((array)$inspection as $key => $value) {
print '<tr><td>' . htmlspecialchars($key) . '</td><td>' . htmlspecialchars($value) . '</td></tr>';
}
print "</table>";
Search
<?php
/**
*
* This example demonstrates using the API to find all owners in QLD modified since a specific date
*
*/
// build API request
$apiRequest = array(
"username" => "user@example.com",
"password" => "bWy1P2xRdyczAzmfxmUYpPjPc4Vg1",
"actions" => array(
array(
"name" => "search",
"params" => array(
"entity" => "owner",
"terms" => array("state" => "Q%"),
"modified_since" => "2011-01-01 10:30:00"
)
)
)
);
$apiRequestData = json_encode($apiRequest);
// send using cURL
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, "https://admin.inspectionapps.com/services/api");
curl_setopt($curlHandle, CURLOPT_POST, true); // set HTTP request method to POST
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // tell cURL to return response data, instead of sending to stdout
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $apiRequestData); // set post data
$apiResponseData = curl_exec($curlHandle);
curl_close($curlHandle);
// decode JSON
$apiResponse = @json_decode($apiResponseData);
if ($apiResponse == null)
die("Error processing server response: " . $apiResponseData);
// check for error
if (!$apiResponse->success)
die('Error on server: ' . $apiResponse->error);
// output results
$owners = $apiResponse->results[0];
print "<ul>";
foreach ($owners as $owner) {
$name = $owner->business_name ? $owner->business_name : "$owner->first_name $owner->last_name";
print "<li>" . htmlspecialchars($name) . "</li>";
}
print "</ul>";
Update Owner
<?php
/**
*
* This example demonstrates using the API to update an owner's phone number
*
*/
// build API request
$apiRequest = array(
"username" => "user@example.com",
"password" => "bWy1P2xRdyczAzmfxmUYpPjPc4Vg1",
"actions" => array(
array(
"name" => "update",
"params" => array(
"entity" => "owner",
"id" => 10,
"values" => array("mobile_phone" => "0424 424 444")
)
)
)
);
$apiRequestData = json_encode($apiRequest);
// send using cURL
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, "https://admin.inspectionapps.com/services/api");
curl_setopt($curlHandle, CURLOPT_POST, true); // set HTTP request method to POST
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // tell cURL to return response data, instead of sending to stdout
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $apiRequestData); // set post data
$apiResponseData = curl_exec($curlHandle);
curl_close($curlHandle);
// decode JSON
$apiResponse = @json_decode($apiResponseData);
if ($apiResponse == null)
die("Error processing server response: " . $apiResponseData);
// check for error
if (!$apiResponse->success)
die('Error on server: ' . $apiResponse->error);
// output results
$owner = $apiResponse->results[0];
print "phone set to: " . htmlspecialchars($owner->mobile_phone);
Add Job
<?php
/**
*
* This example demonstrates using the API to add a new job
*
*/
// build API request
$apiRequest = array(
"username" => "user@example.com",
"password" => "bWy1P2xRdyczAzmfxmUYpPjPc4Vg1",
"actions" => array(
array("name" => "add", "params" => array(
"entity" => "job",
"values" => array(
"address_one" => "addr 1",
"suburb" => 'location2',
"postcode" => 'location3',
"state" => 'location_state',
"country" => "Australia",
"tenant_names" => 'tenant1',
"tenant_phone" => 'tenant2',
"initial_contact_date" => date('Y-m-d H:i:s'),
"inspection" => array(
'additional_instructions' => 'Hello World'
),
"owner" => array(
"business_name" => "foo"
),
"representative" => array(
"business_name" => "bar"
)
)
))
)
);
$apiRequestData = json_encode($apiRequest);
// send using cURL
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, "https://admin.inspectionapps.com/services/api");
curl_setopt($curlHandle, CURLOPT_POST, true); // set HTTP request method to POST
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // tell cURL to return response data, instead of sending to stdout
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $apiRequestData); // set post data
$apiResponseData = curl_exec($curlHandle);
curl_close($curlHandle);
// decode JSON
$apiResponse = @json_decode($apiResponseData);
if ($apiResponse == null)
die("Error processing server response: " . $apiResponseData);
// check for error
if (!$apiResponse->success)
die('Error on server: ' . $apiResponse->error);
// output results
$job = $apiResponse->results[0];
if (!$job)
die("server returned null");
print "<h1>New job:</h1>";
print "<table border=1 cellspacing=0 cellpadding=4>";
foreach ((array)$job as $key => $value) {
print '<tr><td>' . htmlspecialchars($key) . '</td><td>' . htmlspecialchars($value) . '</td></tr>';
}
print "</table>";
Inspection Apps API Documentation « Return to Index