Back

Step by Step Guide to Building a SafetyCulture API Integration in PHP

Aug 17, 20245 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment up and running (I know you've probably got this sorted already)
  • A SafetyCulture API key (if you don't have one, head over to their developer portal and snag one)

Setting up the project

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.

Authentication

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!

Making API requests

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);

Handling API responses

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(); }

Implementing key features

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()); }

Optimizing API usage

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); });

Testing the integration

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); } }

Conclusion

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!

Additional resources

Now go forth and integrate with confidence! Happy coding!