Back

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

Aug 16, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of FareHarbor API integration? You're in for a treat. FareHarbor's API is a powerful tool that lets you tap into their booking system, opening up a world of possibilities for your applications. Whether you're looking to display available tours, make bookings, or manage existing reservations, this guide will get you up and running in no time.

Prerequisites

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

  • A PHP environment (you've got this, right?)
  • FareHarbor API credentials (if you don't have these yet, reach out to FareHarbor)
  • Guzzle HTTP client (trust me, it'll make your life easier)

Authentication

First things first, let's get you authenticated:

$apiKey = 'your-api-key'; $appKey = 'your-app-key'; $headers = [ 'X-FareHarbor-API-App' => $appKey, 'X-FareHarbor-API-User' => $apiKey, ];

Easy peasy! Just replace those placeholder values with your actual keys, and you're good to go.

Basic API Request Structure

Here's the bread and butter of working with the FareHarbor API:

$client = new GuzzleHttp\Client([ 'base_uri' => 'https://fareharbor.com/api/external/v1/', 'headers' => $headers, ]); $response = $client->request('GET', 'companies/');

See how simple that is? We're just setting up our client with the base URL and headers, then making a request. Easy!

Implementing Core Functionalities

Retrieving Available Items

Want to see what's on offer? Here's how:

$response = $client->request('GET', 'companies/COMPANY-SHORTNAME/items/'); $items = json_decode($response->getBody(), true);

Checking Availability

Let's see what times are available for a specific item:

$response = $client->request('GET', 'companies/COMPANY-SHORTNAME/items/ITEM-ID/availability/date/YYYY-MM-DD/'); $availability = json_decode($response->getBody(), true);

Creating Bookings

Time to make a booking! Here's the gist:

$bookingData = [ 'contact' => [ 'name' => 'John Doe', 'email' => '[email protected]', ], 'customers' => [ ['customer_type_rate' => 'CUSTOMER-TYPE-RATE-ID'], ], ]; $response = $client->request('POST', 'companies/COMPANY-SHORTNAME/items/ITEM-ID/availabilities/AVAILABILITY-ID/bookings/', [ 'json' => $bookingData, ]);

Managing Bookings

Need to update or cancel a booking? No sweat:

// Update $response = $client->request('PUT', 'companies/COMPANY-SHORTNAME/bookings/BOOKING-ID/', [ 'json' => $updatedBookingData, ]); // Cancel $response = $client->request('DELETE', 'companies/COMPANY-SHORTNAME/bookings/BOOKING-ID/');

Handling API Responses

Always remember to handle those responses:

try { $response = $client->request('GET', 'companies/'); $data = json_decode($response->getBody(), true); // Do something with $data } catch (GuzzleHttp\Exception\RequestException $e) { // Handle the error echo "Oops! " . $e->getMessage(); }

Implementing Webhooks

If you're feeling adventurous, set up a webhook endpoint:

$rawPayload = file_get_contents('php://input'); $payload = json_decode($rawPayload, true); // Process the webhook data // ... http_response_code(200);

Best Practices

A few pro tips to keep in mind:

  • Respect rate limits (FareHarbor will thank you)
  • Cache responses when it makes sense (your users will thank you)
  • Always validate and sanitize input (your security team will thank you)

Testing and Debugging

Don't forget to use FareHarbor's sandbox environment for testing. It's like a playground, but for code!

If you run into issues, double-check your API credentials, ensure your requests are properly formatted, and don't be afraid to dive into those error messages. They're there to help!

Conclusion

And there you have it! You're now armed with the knowledge to build a solid FareHarbor API integration. Remember, the API documentation is your friend, so keep it bookmarked.

Now go forth and code! Your awesome FareHarbor-powered application awaits. Happy coding!