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.
Before we jump in, make sure you've got these basics covered:
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.
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!
Want to see what's on offer? Here's how:
$response = $client->request('GET', 'companies/COMPANY-SHORTNAME/items/'); $items = json_decode($response->getBody(), true);
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);
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, ]);
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/');
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(); }
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);
A few pro tips to keep in mind:
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!
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!