PHP
ទំព័រនេះត្រូវបានបកប្រែពីភាសាអង់គ្លេស។ ប្រសិនបើទាំងពីរខុសគ្នា ទំព័រភាសាអង់គ្លេសគឺត្រឹមត្រូវ។ អានទំព័រភាសាអង់គ្លេស
បណ្ណាល័យ PHP គឺ spinningcatstudios/lingara នៅលើ Packagist។ វាត្រូវការ PHP 8.2 ឬថ្មីជាងនេះ ហើយទំនាក់ទំនងតាម HTTP តាមរយៈ PSR-18 ដូច្នេះវាដំណើរការជាមួយ Symfony HttpClient, Guzzle ឬក្លាយអិនផ្ទាល់ខ្លួនរបស់អ្នក។
របៀបប្រើវា
- ការហៅគឺធ្វើសមកាលកម្ម។ មធ្យោបាយតែមួយគត់ដើម្បីបោះបង់គឺឈប់អាន៖ ចាកចេញពី
foreachឬហៅclose()លើស្ទ្រីម។ - ស្ទ្រីមត្រូវបានអានដោយ
foreachតែម្តង។ ផ្គូផ្គងព្រឹត្តិការណ៍នីមួយៗដោយinstanceof។ - កំហុសអនុវត្ត
Lingara\Exception\LingaraException។ លើកំហុស APIerrorCode()គឺជាកូដថេរ ហើយgetCode()គឺជាស្ថានភាព HTTPretryAfter()គឺជាចំនួនវិនាទី ឬnull។ - ជ្រើសរើស stack HTTP ដោយអាគុយម៉ង់
http។ ជាមួយ Guzzle ស្ទ្រីមតម្រូវឱ្យបើកallow_url_fopen។ ជាមួយក្លាយអិន PSR-18 ផ្ទាល់ខ្លួនរបស់អ្នក ការស្ទ្រីម និងការកំណត់ពេល គឺជាការងាររបស់អ្នកក្នុងការកំណត់រចនាសម្ព័ន្ធ។ - នៅក្រោម PHP-FPM worker នីមួយៗចាប់ផ្តើមទទេ ដូច្នេះសូមផ្តល់
tokenCachePSR-16 ដល់ក្លាយអិន។ បើគ្មានវាទេ 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";
}
}