Microsoft Translator Azure使用PHP API返回null

时间:2019-01-30 05:57:11

标签: php azure microsoft-translator

输出为null,PHP版本为5.6。我将该行添加到了PHP.INI文件中。 我已经尝试使用HTTP和HTTP,但是它仍然显示null。我更新了主机地址,以包含API调用URL,如azure控制面板中所示。并且没有太多关于人们收到此错误的信息。

<?php

// NOTE: Be sure to uncomment the following line in your php.ini file.
// ;extension=php_openssl.dll

// **********************************************
// *** Update or verify the following values. ***
// **********************************************

// Replace the subscriptionKey string value with your valid subscription key.
$key = 'KEY_REMOVED';

$host = "https://southeastasia.api.cognitive.microsoft.com/sts/v1.0/issuetoken";
$path = "/translate?api-version=3.0";

// Translate to German and Italian.
$params = "&to=de&to=it";

$text = "Hello, world!";

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);
    return $result;
}

$requestBody = array (
    array (
        'Text' => $text,
    ),
);
$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 = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $json;
?>

1 个答案:

答案 0 :(得分:0)

众所周知,您的代码是从官方文档Quickstart: Translate text with the Translator Text REST API (PHP)复制而来的。

根据引用Translator Text API v3.0的{​​{3}}部分,$host值应为以下列表之一。

Base URLs

因此,您可以在代码中使用$host = "https://api.cognitive.microsofttranslator.com";,而无需进行任何更改。这是第一个问题。

第二,enter image description here标头取决于您的Cognitive Services订阅的API类型。

  1. 如果您的API类型为Translator Text,如下图所示,您将不会更改源代码的函数$headers中的任何Translate代码,该代码取决于您的位置,例如为southeastasia

Authentication

  1. 如果您的API类型为All Cognitive Services,如下图所示,则需要在Ocp-Apim-Subscription-Region代码中添加标头$headers

enter image description here

$headers = "Content-type: application/json\r\n" .
        "Content-length: " . strlen($content) . "\r\n" .
        "Ocp-Apim-Subscription-Key: $key\r\n" .
        "Ocp-Apim-Subscription-Region: southeastasia\r\n" .
        "X-ClientTraceId: " . com_create_guid() . "\r\n";

注意 :文档中存在如下问题。 enter image description here

然后,在我的环境中,PHP版本为7.2.12,我运行php -f test.php可以正常工作,并针对上述两种不同情况返回json响应,如下所示。

enter image description here

[
    {
        "detectedLanguage": {
            "language": "en",
            "score": 1
        },
        "translations": [
            {
                "text": "Hallo Welt!",
                "to": "de"
            },
            {
                "text": "Salve, mondo!",
                "to": "it"
            }
        ]
    }
]