Lingara Lingara Dokumentasie Gidse API Biblioteke Apps Bou Webtoepassing
Taal: Afrikaans

PHP

Hierdie bladsy is uit Engels vertaal. As die twee verskil, is die Engelse bladsy korrek. Lees die Engelse bladsy

Die PHP-biblioteek is spinningcatstudios/lingara op Packagist. Dit vereis PHP 8.2 of later en praat HTTP deur PSR-18, so dit werk met Symfony HttpClient, Guzzle of ‘n kliënt van jou eie.

Hoe dit lees

  • Oproepe is sinchronies. Die enigste manier om te kanselleer, is om op te hou lees: verlaat die foreach, of roep close() op die stroom aan.
  • ‘n Stroom word een keer met foreach gelees. Pas elke gebeurtenis met instanceof.
  • Foute implementeer Lingara\Exception\LingaraException. By ‘n API-fout is errorCode() die stabiele kode en getCode() die HTTP-status; retryAfter() is ‘n aantal sekondes, of null.
  • Kies die HTTP-stapel met die http-argument. Met Guzzle het strome allow_url_fopen aangeskakel nodig. Met ‘n PSR-18-kliënt van jou eie is stroming en uitteltye joune om te konfigureer.
  • Onder PHP-FPM begin elke werker leeg, so gee die kliënt ‘n PSR-16-tokenCache. Sonder een ruil elke werker sy eie token, en die token-eindpunt se uurlimiet word deur hulle almal gedeel.

Die voorbeelde hieronder installeer die pakket, skep ‘n kliënt, stroom ‘n woordelys en hanteer ‘n fout.

PHP

composer require spinningcatstudios/lingara:0.1.0-alpha.6

PHP

use Lingara\Client;

$client = new Client(clientId: $clientId, clientSecret: $clientSecret);

PHP

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

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

PHP

use Lingara\Exception\ApiException;
use Lingara\Exception\LingaraException;
use Lingara\Exception\MaintenanceException;
use Lingara\Exception\OAuthException;
use Lingara\Exception\TransportException;

try {
    $client->getUsage();
} catch (LingaraException $e) {
    if ($e instanceof ApiException) {
        // A refusal from the API: status, errorCode (stable) and message (localised).
        echo $e->status(), ' ', $e->errorCode(), ': ', $e->getMessage(), "\n";
        echo 'retry after: ', $e->retryAfter() ?? 'not given', "\n";
    } elseif ($e instanceof OAuthException) {
        // The token endpoint refused the credentials or the scopes.
        echo $e->status(), ' ', $e->error(), ': ', $e->description(), "\n";
    } elseif ($e instanceof MaintenanceException) {
        echo 'under maintenance; retry after: ', $e->retryAfter() ?? 'not given', "\n";
    } elseif ($e instanceof TransportException) {
        // No usable answer: connect, tls, reset, timeout, and so on.
        echo 'transport: ', $e->kind()->value, "\n";
    }
}

Wat hierdie bladsy omvou