<?php 

require 'vendor/autoload.php'; // Adjust the path as needed 

use GuzzleHttp\Client; 
use GuzzleHttp\Psr7\Request; 
use GuzzleHttp\Exception\GuzzleException; 

$client = new Client();

$headers = [
    'X-OAI-API-KEY' => 'jvqrfdwnt70lxea4u1ym28p6b3szkhco', // Replace with your actual API key
    'Accept' => 'application/json',
    'Content-Type' => 'application/json' // Ensure the content type is set to JSON
];

// Receive content from AJAX request
$data = json_decode(file_get_contents('php://input'), true);
$content = $data['content']; // Replace 'xxx' with the actual content received

$body = json_encode([
    "content" => $content, 
    "title" => "optional title", // Optional title for the scan
    "aiModelVersion" => "2.0", // AI model version
    "storeScan" => "false" 
]);

$request = new Request('POST', 'https://api.originality.ai/api/v1/scan/ai', $headers, $body); 

try {
    $res = $client->send($request);
    $responseBody = json_decode($res->getBody(), true);

    if (isset($responseBody['success']) && $responseBody['success'] === true) {
        // Extract and display the score
        $score = $responseBody['score'] ?? null; 
        if ($score !== null) {
            echo "AI Score: Originality - " . $score['original'] . ", AI Content - " . $score['ai'] . "\n";
        } else {
            echo "Score not available in the response.\n";
        }
    } else {
        echo "Error or unsuccessful response from Originality.AI.\n";
    }
} catch (GuzzleException $e) {
    echo "Request failed: " . $e->getMessage();
}

?>
