Hey there, fellow developer! Ready to dive into the world of SafetyCulture API integration? You're in for a treat. This guide will walk you through building a robust integration in PHP, allowing you to tap into the power of SafetyCulture's inspection data and functionality. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get our project structure in order:
mkdir safetyculture-integration
cd safetyculture-integration
composer init
composer require guzzlehttp/guzzle
We're using Guzzle to handle our HTTP requests because, let's face it, it makes life a whole lot easier.
Alright, let's get that API key working for us:
<?php require 'vendor/autoload.php'; $client = new GuzzleHttp\Client([ 'base_uri' => 'https://api.safetyculture.io/audits/v1/', 'headers' => [ 'Authorization' => 'Bearer YOUR_API_KEY_HERE' ] ]);
Replace YOUR_API_KEY_HERE
with your actual API key, and you're good to go!
Now for the fun part - let's start making some requests:
// GET request example $response = $client->request('GET', 'audits'); $audits = json_decode($response->getBody(), true); // POST request example $response = $client->request('POST', 'audits', [ 'json' => [ 'template_id' => 'template_123abc', 'audit_title' => 'My New Inspection' ] ]); $newAudit = json_decode($response->getBody(), true);
Always expect the unexpected:
try { $response = $client->request('GET', 'audits'); $audits = json_decode($response->getBody(), true); } catch (GuzzleHttp\Exception\RequestException $e) { echo "Oops! Something went wrong: " . $e->getMessage(); }
Let's put it all together and create some useful functions:
function getInspections($client) { $response = $client->request('GET', 'audits'); return json_decode($response->getBody(), true); } function updateInspectionStatus($client, $auditId, $status) { $response = $client->request('PUT', "audits/$auditId", [ 'json' => ['status' => $status] ]); return json_decode($response->getBody(), true); } function downloadInspectionReport($client, $auditId) { $response = $client->request('GET', "audits/$auditId/report"); file_put_contents("report_$auditId.pdf", $response->getBody()); }
Remember, with great power comes great responsibility. Be mindful of rate limits and consider implementing caching for frequently accessed data:
use Symfony\Component\Cache\Adapter\FilesystemAdapter; $cache = new FilesystemAdapter(); $inspections = $cache->get('inspections', function() use ($client) { return getInspections($client); });
Don't forget to test your code! Here's a quick PHPUnit example:
class SafetyCultureIntegrationTest extends PHPUnit\Framework\TestCase { public function testGetInspections() { $client = $this->createMock(GuzzleHttp\Client::class); $client->method('request') ->willReturn(new GuzzleHttp\Psr7\Response(200, [], json_encode(['audits' => []]))); $inspections = getInspections($client); $this->assertIsArray($inspections); } }
And there you have it! You've just built a solid foundation for your SafetyCulture API integration. From here, you can expand on these concepts to create more complex and powerful integrations. Remember, the sky's the limit!
Now go forth and integrate with confidence! Happy coding!