PHP
यह पेज अंग्रेज़ी से अनुवादित है। अगर दोनों में अंतर हो, तो अंग्रेज़ी पेज सही है। अंग्रेज़ी पेज पढ़ें
PHP लाइब्रेरी Packagist पर spinningcatstudios/lingara है। इसके लिए PHP 8.2 या उसके बाद का संस्करण चाहिए और यह PSR-18 के ज़रिए HTTP पर बात करती है, इसलिए यह Symfony HttpClient, Guzzle या आपके अपने क्लाइंट के साथ काम करती है।
इसे कैसे इस्तेमाल करें
- कॉल सिंक्रोनस होती हैं। रद्द करने का एकमात्र तरीका पढ़ना बंद करना है:
foreachसे बाहर निकलें, या स्ट्रीम परclose()कॉल करें। - स्ट्रीम को
foreachसे एक बार पढ़ा जाता है। हर इवेंट कोinstanceofसे मिलाएँ। - एरर
Lingara\Exception\LingaraExceptionको इम्प्लीमेंट करते हैं। API एरर पर,errorCode()स्थिर कोड है औरgetCode()HTTP स्टेटस है;retryAfter()सेकंड की एक संख्या है, याnull। httpआर्ग्युमेंट से HTTP स्टैक चुनें। Guzzle के साथ, स्ट्रीम के लिएallow_url_fopenसक्षम होना चाहिए। आपके अपने PSR-18 क्लाइंट के साथ, स्ट्रीमिंग और टाइमआउट कॉन्फ़िगर करना आपकी ज़िम्मेदारी है।- PHP-FPM के तहत हर worker खाली शुरू होता है, इसलिए क्लाइंट को एक PSR-16
tokenCacheदें। इसके बिना, हर worker अपना टोकन खुद एक्सचेंज करता है, और टोकन एंडपॉइंट की प्रति घंटा सीमा उन सब में साझा होती है।
नीचे दिए उदाहरण पैकेज इंस्टॉल करते हैं, क्लाइंट बनाते हैं, शब्दावली सूची स्ट्रीम करते हैं और एक एरर संभालते हैं।
PHP
composer require spinningcatstudios/lingara:0.1.0-alpha.6PHP
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";
}
}