Back

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

Aug 16, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of customer feedback with Delighted's API? You're in for a treat. This guide will walk you through integrating Delighted's powerful survey tools into your PHP application. Let's get cracking!

Prerequisites

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

  • PHP 7.4 or higher (come on, you're not still using 5.6, right?)
  • Composer installed (because who wants to manage dependencies manually?)
  • A Delighted API key (grab one from your Delighted dashboard)

Setting up the project

First things first, let's get that Delighted PHP library installed:

composer require delighted/delighted-php

Now, let's initialize the Delighted client:

use Delighted\Client; $client = new Client('your-api-key-here');

Easy peasy, right?

Basic API Operations

Creating a survey response

Let's send out some surveys:

$response = $client->createSurveyResponse([ 'email' => '[email protected]', 'score' => 9 ]);

Retrieving survey responses

Want to see what your customers are saying?

$responses = $client->listSurveyResponses();

Updating a survey response

Oops, made a mistake? No worries:

$client->updateSurveyResponse($responseId, [ 'score' => 10 ]);

Deleting a survey response

Sometimes you just need to start fresh:

$client->deleteSurveyResponse($responseId);

Advanced Features

Filtering and pagination

Don't drown in data, filter it:

$responses = $client->listSurveyResponses([ 'per_page' => 50, 'page' => 2, 'since' => '2023-01-01' ]);

Handling rate limits

Be nice to the API, it's your friend:

try { // Your API calls here } catch (\Delighted\RateLimitedException $e) { sleep($e->getRetryAfter()); // Retry your request }

Error handling and exceptions

Always be prepared:

try { // Your API calls here } catch (\Delighted\RequestException $e) { echo "Oops! " . $e->getMessage(); }

Webhooks Integration

Setting up webhook endpoints

Listen for updates:

// In your webhook endpoint $payload = file_get_contents('php://input'); $data = json_decode($payload, true); // Process the webhook data

Processing webhook payloads

Do something cool with that data:

if ($data['event'] === 'survey_response.created') { // Handle new survey response }

Best Practices

  • Keep your API key secret (use environment variables, not hard-coded strings)
  • Cache responses when possible to reduce API calls
  • Use asynchronous processing for non-time-sensitive operations

Testing and Debugging

Unit test your integration:

public function testCreateSurveyResponse() { $client = new Client('test-api-key'); $response = $client->createSurveyResponse([ 'email' => '[email protected]', 'score' => 9 ]); $this->assertEquals(9, $response['score']); }

Conclusion

And there you have it! You're now equipped to integrate Delighted's API into your PHP application like a pro. Remember, the key to great integrations is clean code and smart error handling. Now go forth and gather that valuable customer feedback!

Need more info? Check out Delighted's API documentation for the nitty-gritty details.

Happy coding!