PHP

<?php

$token = "YOUR_TOKEN_HERE"; // Replace with your actual token
$createUrl = "https://api.autocontentapi.com/Content/Create";
$statusBaseUrl = "https://api.autocontentapi.com/content/status/";
$pollInterval = 5; // poll every 5 seconds

// The request data to create the content
$requestData = [
    "resources" => [
        ["content" => "https://autocontentapi.com", "type" => "website"],
        ["content" => "74% of businesses expect AI to increase productivity", "type" => "text"]
    ],
    "text" => "Explain why automating with AutoContent API is a game changer for any business",
    "outputType" => "audio"
];

function createContent($token, $createUrl, $requestData) {
    $ch = curl_init($createUrl);
    $headers = [
        "Authorization: Bearer $token",
        "Content-Type: application/json",
        "Accept: text/plain"
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        echo "Curl error: " . curl_error($ch) . "\n";
        curl_close($ch);
        return null;
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode >= 200 && $httpCode < 300) {
        return json_decode($response, true);
    } else {
        echo "HTTP error: $httpCode\nResponse: $response\n";
        return null;
    }
}

function getStatus($token, $url) {
    $ch = curl_init($url);
    $headers = [
        "Authorization: Bearer $token",
        "Content-Type: application/json",
        "Accept: application/json"
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        echo "Curl error: " . curl_error($ch) . "\n";
        curl_close($ch);
        return null;
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode >= 200 && $httpCode < 300) {
        return json_decode($response, true);
    } else {
        echo "HTTP error: $httpCode\nResponse: $response\n";
        return null;
    }
}

function pollStatus($token, $statusBaseUrl, $requestId, $pollInterval) {
    $statusUrl = $statusBaseUrl . $requestId;

    while (true) {
        $data = getStatus($token, $statusUrl);
        if (!$data) {
            return;
        }

        if (!empty($data["error_message"])) {
            echo "Error from status check: " . $data["error_message"] . "\n";
            return;
        }

        if (isset($data["status"]) && $data["status"] === 100) {
            echo "Content creation complete!\n";
            echo "Audio URL: " . $data["audio_url"] . "\n";
            echo "Audio Title: " . $data["audio_title"] . "\n";
            return;
        }

        echo "Current status: " . $data["status"] . ". Waiting for 100...\n";
        sleep($pollInterval);
    }
}

// Main Execution
$createResponse = createContent($token, $createUrl, $requestData);
if (!$createResponse) {
    exit("Failed to create content.\n");
}

$requestId = $createResponse["request_id"] ?? null;
$errorMessage = $createResponse["error_message"] ?? null;

if ($errorMessage || !$requestId) {
    echo "Error from create request: " . $errorMessage . "\n";
    exit(1);
}

echo "Request initiated. Request ID: $requestId\n";
pollStatus($token, $statusBaseUrl, $requestId, $pollInterval);

Last updated