API Details: Travel Review Sentiment Checker AI API
Description: Shows how to use the travelReviewSentiment() method to analyze the sentiment of a travel or hospitality product review.
Code Snippet:
<?php
require 'vendor/autoload.php';
use SharpAPI\SharpApiService;
// Load environment variables
$apiKey = getenv('SHARP_API_KEY');
if (!$apiKey) {
throw new Exception("API key not found. Please set SHARP_API_KEY in your environment variables.");
}
// Initialize the SharpApiService
$sharpApi = new SharpApiService($apiKey);
$travelReview = "The trip was " . (rand(0, 1) ? "amazing" : "disappointing") . ". " . bin2hex(random_bytes(5));
try {
$statusUrl = $sharpApi->travelReviewSentiment($travelReview);
$resultJob = $sharpApi->fetchResults($statusUrl);
echo "Travel Review Sentiment Result:\n";
print_r(json_decode($resultJob->getResultJson(), true));
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}$resultJob->getResultJson() format example:
{
"data": {
"type": "api_job_result",
"id": "f85b7ac5-33cd-4796-8935-dc8c22219946",
"attributes": {
"status": "success",
"type": "tth_review_sentiment",
"result": {
"score": 95,
"opinion": "POSITIVE"
}
}
}
}