Microsoft翻译文本已停止工作-PHP

Microsoft翻译文本已停止工作-PHP,php,azure,microsoft-cognitive,azure-cognitive-services,Php,Azure,Microsoft Cognitive,Azure Cognitive Services,我一直在使用此库使用Microsoft认知翻译器翻译文本 我有一个Azure帐户,我认为我的端点和密钥仍然有效。虽然我有“免费”套餐,但在此期间我没有做任何改变 我曾经翻译过多种语言的代码,以前工作得很好。由于某种原因,它最近停止了工作。我编辑了这篇文章以设置: $host = "https://api.cognitive.microsofttranslator.com", thank you (typo). 如果我将$text设置为以下简单值,它实际上会起作用: $text = 'Gute

我一直在使用此库使用Microsoft认知翻译器翻译文本

我有一个Azure帐户,我认为我的端点和密钥仍然有效。虽然我有“免费”套餐,但在此期间我没有做任何改变

我曾经翻译过多种语言的代码,以前工作得很好。由于某种原因,它最近停止了工作。我编辑了这篇文章以设置:

$host = "https://api.cognitive.microsofttranslator.com", thank you (typo).
如果我将$text设置为以下简单值,它实际上会起作用:

$text = 'Guten Morgen!  Mit Corona zusammenraufen.Milde gesagt: Es sind keine besonders beruhigenden Nachrichten.';
返回英语:

{"text":"Good morning! To put it mildly, it's not particularly reassuring news.","to":"en"}
如果我将字符串稍微延长到:

$text = 'Guten Morgen!  Mit Corona zusammenraufen.  Milde gesagt: Es sind keine besonders beruhigenden Nachrichten. Milde gesagt: Es sind keine besonders beruhigenden Nachrichten.';
我得到一个:

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
这在以前使用更长的字符串进行输入时起作用。我对Azure有一个“免费”计划。我的Azure帐户现在是否可能存在字符串长度限制

我的密码是。有一些额外的东西可以将翻译放入数据库

$key = 'MicrosoftKEY';
$host = "https://api.cognitive.microsofttranslator.com";
$path = "/translate?api-version=3.0";
$languages = $data['languagearray'];  // array of languages to translate to.
$params = '&from=' . $data["from"] ;  // language to translate from
foreach ($languages as $language) {
$params .= "&to=" . $language;
}
$params .= "&textType=html";
$text = $data["text"];  // text to translate

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 to return the translated text, seem below, $json = jsonp_decode($result, true)[0]["translations"];

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'] . '<br>';
    echo $language['text'] . '<br>';
}
$key='MicrosoftKEY';
$host=”https://api.cognitive.microsofttranslator.com";
$path=“/translate?api版本=3.0”;
$languages=$data['languagearray'];//要转换为的语言数组。
$params='&from='$数据[“来自”];//翻译语言
foreach($languages作为$language){
$params.=“&to=”.$language;
}
$params.=“&textType=html”;
$text=$data[“text”];//要翻译的文本
如果(!function_存在('com_create_guid')){
函数com_创建_guid(){
返回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)
);
}
}
//函数返回翻译后的文本,如下所示,$json=jsonp_decode($result,true)[0][“translations”];
函数转换($host、$path、$key、$params、$content){
$headers=“内容类型:application/json\r\n”。
“内容长度:”.strlen($Content)。“\r\n”。
“Ocp Apim订阅密钥:$Key\r\n”。
“X-ClientTraceId:”.com\u create\u guid()。“\r\n”;
//注意:即使发出HTTPS请求,也要使用密钥“http”。请参阅:
// http://php.net/manual/en/function.stream-context-create.php
$options=array(
“http'=>数组(
'header'=>$headers,
'方法'=>'发布',
“内容”=>$content
)
);
$context=stream\u context\u create($options);
$result=file_get_contents($host.$path.$params,false,$context);
回声$结果;
返回$result;
}
$requestBody=数组(
排列(
“Text”=>$Text,
),
);
函数jsonp_decode($jsonp,$assoc=false){//PHP5.3将深度作为第三个参数添加到json_decode中
如果($jsonp[0]!='['&&$jsonp[0]!='{'){//我们有jsonp
$jsonp=substr($jsonp,strpos($jsonp,“(”));
}
返回json_解码(trim($jsonp,,();'),$assoc);
}
$content=json_encode($requestBody);
$result=Translate($host、$path、$key、$params、$content);
//注意:我们将结果(即JSON)转换为对象和对象之间的转换,以便可以漂亮地打印它。
//我们希望避免转义结果包含的任何Unicode字符。请参阅:
// http://php.net/manual/en/function.json-encode.php
$json=jsonp_decode($result,true)[0][“translations”];
$conn=DatabaseFactory::getFactory()->getConnection();
foreach($json作为$language){
$query='UPDATE kronen_translations SET translated_text=?其中语言_code=?';
$parameters=[$language['text'],$language['to'];
$stmt=$conn->prepare($query);
$stmt->execute($parameters);
echo$language['to'].
; echo$language['text']。
; }
我做了更多的调查,最终与Microsoft认知服务公司达成协议。几个月前,该脚本运行良好,我实际上正在将几段文字翻译成大约40种不同的语言(几乎所有他们支持的语言)使用一个捆绑的呼叫,一个呼叫,我会得到一个回复,并拥有所有的翻译。这在以前工作得很好

但是,翻译服务的API请求有一个字符限制:

见:

"每个翻译请求限制为5000个字符,适用于您要翻译的所有目标语言。例如,发送1500个字符的翻译请求以将其翻译为3种不同的语言会导致请求大小为1500x3=4500个字符,这满足了请求限制。您按字符收费,而不是按数字收费请求数量。建议发送较短的请求。“

有40种不同的语言,有几段文字有几百个字符,我已经超过了限制。在与MS支持部门进行电子邮件交易后,发现API请求限制在去年的部分时间里没有得到执行,最近由于他们端上的一个bug,他们最近显然已经修复了它

仅供参考,旧代码如下所示,查询字符串大约有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'] . '<br>';
    echo $language['text'] . '<br>';
}
$languages=$data['languagearray'];
$params='&from='。$data[“from”];
foreach($languages作为$language){
$params.=“&to=”.$language;
}
$params.=“&textType=html”;
$text=$data[“text”];
如果(!function_存在('com_create_guid')){
函数com_创建_guid(){
返回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)
);
}
}
函数转换($host、$path、$key、$params、$content){
$headers=“内容类型:application/json\r\n”。
“内容长度:”.strlen($Content)。“\r\n”。
“Ocp Apim订阅密钥:$Key\r\n”。
“X-ClientTraceId:”.com\u create\u guid()。“\r\n”;
//注意:使用“http”键
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'] . '<br>';
    echo $language['text'] . '<br>';
}