Microsoft Translator Azure使用PHP API返回null [英] Microsoft translator azure are returning null with PHP API
问题描述
输出为null,PHP版本为5.6.我将这一行添加到了PHP.INI文件中. 我已经尝试过使用HTTP和HTTP,但是它仍然显示null.我更新了主机地址,以包含API调用URL,如azure控制面板中所示.而且没有太多关于人们收到此错误的信息.
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;
?>
推荐答案
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.
因此,您可以在代码中使用$host = "https://api.cognitive.microsofttranslator.com";
而不进行任何更改.这是第一个问题.
So you can use $host = "https://api.cognitive.microsofttranslator.com";
in your code without any changes. It is the first issue.
第二, Authentication
标头取决于您的Cognitive Services订阅的API类型.
Second, the Authentication
header is depended on your API type of your Cognitive Services subscription.
- 如果您的API类型为
Translator Text
,如下图所示,您将不会在原始代码的函数Translate
中更改任何$headers
代码,该值取决于您的位置,例如southeastasia
./li>
- If your API type is
Translator Text
as the figure below, you will not change any$headers
code in the functionTranslate
of the origin code which value is depended on your location, such assoutheastasia
.
- 如果您的API类型为
All Cognitive Services
,如下图所示,则需要在$headers
代码中添加标头Ocp-Apim-Subscription-Region
.
- If your API type is
All Cognitive Services
as the figure below, you need to add a headerOcp-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.
然后,在我的环境中,PHP版本为7.2.12
,我运行了php -f test.php
,它工作正常,并针对上述两种不同情况返回了json响应,如下所示.
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"
}
]
}
]
这篇关于Microsoft Translator Azure使用PHP API返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!