Coding standards
Security in ResourceSpace
Developer reference
Database
User api session functions
Geolocation functions
Storage test functions
Cli functions
Lang translation functions

lang_translation_handle()

Description

Start one timed cURL request and collect its retry delay without logging headers.

Parameters

ColumnTypeDefaultDescription
$api_key string OpenAI API key.
$body: array
$body array Chat Completions request body.

Return

array{handle: CurlHandle, retry_after: stdClass} Handle and response header storage.

Location

include/lang_translation_functions.php lines 434 to 456

Definition

 
function lang_translation_handle(string $api_key, array $body): array
{
    
$retry_after = (object) ['seconds' => 0];
    
$handle = curl_init('https://api.openai.com/v1/chat/completions');
    if (
$handle === false) {
        throw new 
RuntimeException('Could not initialise the translation request');
    }
    
curl_setopt_array($handle, [
        
CURLOPT_RETURNTRANSFER => true,
        
CURLOPT_POST => true,
        
CURLOPT_CONNECTTIMEOUT => 15,
        
CURLOPT_TIMEOUT => 120,
        
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Authorization: Bearer ' . $api_key],
        
CURLOPT_POSTFIELDS => json_encode($body, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
        
CURLOPT_HEADERFUNCTION => static function (CurlHandle $handle, string $header) use ($retry_after): int {
            if (
stripos($header, 'Retry-After:') === 0) {
                
$retry_after->seconds = lang_translation_retry_after(substr($header, 12), time());
            }
            return 
strlen($header);
        },
    ]);
    return [
'handle' => $handle, 'retry_after' => $retry_after];
}

This article was last updated 27th November 2026 18:35 Europe/London time based on the source file dated 27th November 2026 17:15 Europe/London time.