Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save makowskid/8bdb7832d66c7e977dda61b48eb07f27 to your computer and use it in GitHub Desktop.

Select an option

Save makowskid/8bdb7832d66c7e977dda61b48eb07f27 to your computer and use it in GitHub Desktop.
SharpAPI.com PHP Client AI workflow API: Travel Review Sentiment Method Example

SharpAPI.com PHP Client AI workflow API: Travel Review Sentiment Method Example

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"
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment