但是,对于翻译服务的API请求有一个字符限制:
“每个翻译请求限制为5000个字符,跨越您要翻译的所有目标语言。例如,发送1500个字符的翻译请求以翻译成3种不同的语言,结果请求大小为1500x3=4500个字符,这满足了请求限制。按字符收费,而不是按请求数收费。建议发送较短的请求。”
仅供参考,旧代码如下所示,查询字符串大约有40个左右&to=lang&。
$languages = $data['languagearray'];
$params = '&from=' . $data["from"] ;
foreach ($languages as $language) {
$params .= "&to=" . $language;
}
$params .= "&textType=html";
$text = $data["text"];
if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
function Translate ($host, $path, $key, $params, $content) {
$headers = "Content-type: application/json\r\n" .
"Content-length: " . strlen($content) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n" .
"X-ClientTraceId: " . com_create_guid() . "\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path . $params, false, $context);
echo $result;
return $result;
}
$requestBody = array (
array (
'Text' => $text,
),
);
function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
return json_decode(trim($jsonp,'();'), $assoc);
}
$content = json_encode($requestBody);
$result = Translate ($host, $path, $key, $params, $content);
// Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
// We want to avoid escaping any Unicode characters that result contains. See:
// http://php.net/manual/en/function.json-encode.php
$json = jsonp_decode($result, true)[0]["translations"];
$conn = DatabaseFactory::getFactory()->getConnection();
foreach ($json as $language) {
$query = 'UPDATE kronen_translations SET translated_text = ? WHERE language_code = ?';
$parameters = [$language['text'], $language['to']];
$stmt = $conn->prepare($query);
$stmt->execute($parameters);
echo $language['to'] . '
';
echo $language['text'] . '
';
}
你可以进行这样的捆绑调用,然后解码返回的结果。
解决方案是对每种语言进行一系列调用(在本例中一次调用一个),然后对结果进行串行处理。新的代码是这样的。也许有更好的方法,但至少这样我可以处理大小合理的文本块。只需要一点时间就可以运行40种左右语言的脚本,尽管仍然只有30秒,这取决于文本的大小。
function Translate ($host, $path, $key, $params, $content) {
$headers = "Content-type: application/json\r\n" .
"Content-length: " . strlen($content) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n" .
"X-ClientTraceId: " . com_create_guid() . "\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path . $params, false, $context);
echo $result;
return $result;
}
function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
return json_decode(trim($jsonp,'();'), $assoc);
}
$key = 'xxxxx';
$host = "https://api.cognitive.microsofttranslator.com";
$path = "/translate?api-version=3.0";
$languages = $data['languagearray'];
// print_r($languages);
$conn = DatabaseFactory::getFactory()->getConnection();
$text = $data["text"];
foreach ($languages as $language) {
$params = '&from=' . $data["from"] ;
$params .= "&to=" . $language;
$params .= "&textType=html";
$text = $data["text"];
$requestBody = array (
array (
'Text' => $text,
),
);
$content = json_encode($requestBody);
$result = Translate ($host, $path, $key, $params, $content);
$json = jsonp_decode($result, true)[0]["translations"];
foreach ($json as $language) {
$query = 'UPDATE kronen_translations SET translated_text = ? WHERE language_code = ?';
$parameters = [$language['text'], $language['to']];
$stmt = $conn->prepare($query);
$stmt->execute($parameters);
echo $language['to'] . '
';
echo $language['text'] . '
';
}