-->

Microsoft translator azure are returning null with

2019-08-27 05:18发布

问题:

The output is null, PHP version is 5.6. I added the line to the PHP.INI file. I have tried with the HTTP and HTTP's but it still shows the null. I updated the host address to include the API call URL as shown in the azure control panel. And there is not much information about people receiving this error.

<?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:

As I known, your code is copied from the offical document Quickstart: Translate text with the Translator Text REST API (PHP).

According to the section Base URLs of the reference Translator Text API v3.0, the $host value should be the one of the list below.

So you can use $host = "https://api.cognitive.microsofttranslator.com"; in your code without any changes. It is the first issue.

Second, the Authentication header is depended on your API type of your Cognitive Services subscription.

  1. If your API type is Translator Text as the figure below, you will not change any $headers code in the function Translate of the origin code which value is depended on your location, such as southeastasia.

  1. If your API type is All Cognitive Services as the figure below, you need to add a header Ocp-Apim-Subscription-Region in the $headers code.

$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";

Note: there is an issue in the document as below.

Then, in my environment, PHP version is 7.2.12, and I run php -f test.php which works fine and return the json response for the two different cases above, as below.

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