PHP code example of spinningcatstudios / lingara

1. Go to this page and download the library: Download spinningcatstudios/lingara library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spinningcatstudios / lingara example snippets


use Lingara\Client;
use Lingara\Model\VocabRequest;
use Lingara\Stream\GenerateVocabularyEvent;

$client = new Client(
    clientId: getenv('LINGARA_CLIENT_ID') ?: null,
    clientSecret: getenv('LINGARA_CLIENT_SECRET') ?: null,
    tokenCache: $psr16Cache,          // optional; strongly advised under PHP-FPM
);

$usage = $client->getUsage();         // ApiResponse: ->value, ->servedVersion

$request = new VocabRequest(['level' => 2, 'source_lang' => 'en', 'target_lang' => 'zh']);
foreach ($client->generateVocabulary($request) as $event) {
    if ($event instanceof GenerateVocabularyEvent\Item) {
        echo $event->data->getWord(), "\n";
    }
}

$stream = $client->streamLessonPlan($planId);
try {
    // … code that may return before the loop …
    foreach ($stream as $event) {
        // …
    }
} finally {
    $stream->close();
}

use Lingara\Events\Webhook;
use Lingara\Events\VerificationException;

$webhook = new Webhook(getenv('LINGARA_WEBHOOK_SECRET')); // or [$new, $old] during a rotation
try {
    $event = $webhook->verify(file_get_contents('php://input'), getallheaders());
} catch (VerificationException $e) {
    http_response_code(400);                              // $e->reason() says why
    exit;
}
http_response_code(204);

$feed = $client->events(cursor: $saved);
foreach ($feed as $event) {
    // …
}
$saved = $feed->cursor();

use Lingara\Events\Generated\InboundEvent;
use Lingara\Model\WorldContextChanged;

$accepted = $client->sendEvent(InboundEvent::worldContextChanged(new WorldContextChanged([
    'scene' => 'A night market after rain', 'source_lang' => 'en', 'target_lang' => 'zh', 'level' => 2, 'generate' => true,
])))->value;