<?php
// Define the API endpoint
$apiUrl = "https://h...content-available-to-author-only...n.app/text/v1/check";

// The text you want to analyze
$text = "Dies ist ein nicht-toxischer Beispieltext.";

// Prepare the data payload
$data = array(
    "text" => $text
);

// Encode the data to JSON
$jsonData = json_encode($data);

// Set your access token here (or load it from file/environment)
$bearerToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiVGVzdGVyIiwicm9sZSI6InRlc3Rfc3Vic2NyaXB0aW9uIiwiZXhwIjoxNzYzMTAzMjQzfQ.njDRUR_GW-IdZ29AOSkcm69N-7zRzX6Zd3mnQL2uekU";

if (!$bearerToken) {
    echo "Error: Bearer token not set. Please set the BEARER_TOKEN environment variable.\n";
    exit;
}

// Initialize cURL and set options
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $bearerToken
));

// Execute the request
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo "cURL Error: " . curl_error($ch) . "<br>";
    curl_close($ch);
    exit;
}

// Get the HTTP status code
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Close the cURL session
curl_close($ch);

// Decode the response
$responseData = json_decode($response, true);

// Response handling
if ($httpStatus == 200) {

	// Successful response
    echo "Request was successful.<br>";

	// This shows the complete response data:
    echo "Response Data:<br>";
    print_r($responseData);

	// This is the estimated toxicity value:
	$toxicity = $responseData['toxicity'];
	echo "Toxicity Level:<br>";
	echo $toxicity;

} else {
    // Error response
    echo "Request failed with status code $httpStatus.<br>";
    if (isset($responseData['detail'])) {
        echo "Error Detail: " . $responseData['detail'] . "<br>";
    } else {
        echo "Response Body:<br>";
        print_r($responseData);
    }
}
?>