Back

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

Aug 2, 20246 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your PHP project with some Trustpilot goodness? You're in the right place. We're going to dive into integrating the Trustpilot API using the awesome ramos94/php-trustpilot-api package. It's a breeze, and you'll be up and running in no time.

Prerequisites

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

  • A PHP environment (you've got this, right?)
  • Composer installed (because who doesn't love dependency management?)
  • Trustpilot API credentials (if you don't have these yet, hop over to Trustpilot and get 'em)

Installation

Let's kick things off by installing our package. Fire up your terminal and run:

composer require ramos94/php-trustpilot-api

Easy peasy, lemon squeezy!

Configuration

Now, let's set up those API credentials and get our Trustpilot client ready to roll:

use Ramos\TrustpilotApi\TrustpilotApi; $apiKey = 'your_api_key_here'; $secret = 'your_secret_here'; $username = 'your_username_here'; $password = 'your_password_here'; $trustpilot = new TrustpilotApi($apiKey, $secret, $username, $password);

Basic Usage

Time to get our hands dirty! Let's fetch some business info and reviews:

// Get business info $businessInfo = $trustpilot->getBusinessInfo('your_business_unit_id'); // Fetch reviews $reviews = $trustpilot->getReviews('your_business_unit_id');

Look at you go! You're already pulling data like a pro.

Advanced Features

Feeling adventurous? Let's post an invitation link and handle a webhook:

// Post invitation link $invitationLink = $trustpilot->postInvitationLink('your_business_unit_id', '[email protected]'); // Handle webhook (in your webhook endpoint) $webhookData = json_decode(file_get_contents('php://input'), true); // Process $webhookData as needed

Error Handling and Best Practices

Don't let rate limits get you down. Implement some basic error handling and caching:

try { $reviews = $trustpilot->getReviews('your_business_unit_id'); } catch (\Exception $e) { if ($e->getCode() == 429) { // Handle rate limit, maybe wait and retry } else { // Handle other errors } } // Simple caching example $cacheKey = 'trustpilot_reviews'; if ($cache->has($cacheKey)) { $reviews = $cache->get($cacheKey); } else { $reviews = $trustpilot->getReviews('your_business_unit_id'); $cache->set($cacheKey, $reviews, 3600); // Cache for 1 hour }

Example Implementation

Here's a quick script tying it all together:

<?php require 'vendor/autoload.php'; use Ramos\TrustpilotApi\TrustpilotApi; $trustpilot = new TrustpilotApi('api_key', 'secret', 'username', 'password'); try { $businessInfo = $trustpilot->getBusinessInfo('business_unit_id'); $reviews = $trustpilot->getReviews('business_unit_id'); echo "Business Name: " . $businessInfo['name'] . "\n"; echo "Total Reviews: " . count($reviews) . "\n"; foreach ($reviews as $review) { echo "Review Title: " . $review['title'] . "\n"; echo "Rating: " . $review['stars'] . " stars\n\n"; } } catch (\Exception $e) { echo "Error: " . $e->getMessage(); }

Testing and Debugging

When things go sideways (and they will, because that's just how coding rolls), the Trustpilot API console is your best friend. Use it to test your requests and responses. And don't forget to implement proper logging – your future self will thank you!

Conclusion

And there you have it! You've just leveled up your PHP skills with a shiny new Trustpilot integration. Remember, the ramos94/php-trustpilot-api package documentation is your trusty sidekick for more advanced features and options.

Now go forth and build something awesome! And if you run into any snags, don't sweat it – that's just part of the fun. Happy coding!