API Details: Summarize Text AI API
Description: Shows how to use the summarizeText() method to generate a summarized version of provided text.
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);
$longText = "Red Bull's Max Verstappen says this weekend's Las Vegas Grand Prix is \"99% show
and 1% sporting event\". \n\n The triple world champion said he is \"not looking forward\"
to the razzmatazz around the race, the first time Formula 1 cars have raced down the city's
famous Strip. \n\n Other leading drivers were more equivocal about the hype.
\n\n Aston Martin's Fernando Alonso said: \"With the investment
that has been made and the place we are racing, it deserves a little bit [of] different treatment.\"
\n\n The weekend was kick-started on Wednesday evening with a lavish opening ceremony.\n\n
It featured performances from several music stars, including Kylie Minogue and Journey, and culminated
in the drivers being introduced to a sparsely populated crowd in light rain by being lifted into view
on hydraulic platforms under a sound-and-light show. \n\n Lewis Hamilton said: \"It's amazing to be here.";
try {
$statusUrl = $sharpApi->summarizeText($longText, 'English', 50, 'Neutral', null);
$resultJob = $sharpApi->fetchResults($statusUrl);
echo "Summarized Text 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": "5fa45f0e-6680-4f64-8528-3f085a87bd2f",
"attributes": {
"status": "success",
"type": "content_summarize",
"result": {
"summary": "Max Verstappen thinks the Las Vegas Grand Prix is more showbiz than sport, while Lewis Hamilton and Fernando Alonso are soaking up the glitz. Expect hydraulic platforms, light shows, and maybe some racing. Don't knock it 'til you try it!"
}
}
}
}