我正在尝试将中文字符翻译成英文,但我认为在编码方面存在问题。
我如何将汉字传递给微软,以便他们翻译并将英文文本返回给我?由于编码,我似乎无法发送汉字?我尝试检测编码类型。
$completeColours 中的汉字
橘色,灰色,深蓝,绿色
URL编码文本%26%2327224%3B%26%2333394%3B%2C%26%2328784%3B%26%2333394%3B%2C%26%2328145%3B%26%2334013%3B%2C%26%2332511%3B %26%2333394%3B
返回结果
橘色,灰色,深蓝,绿色
代码
// Prepare variables
$text = urlencode($completeColours);
echo $text;
$from = 'zh-CHT';
$to = 'en';
// Prepare cURL command
$key = 'xxx';
$ch = curl_init('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate?Text=%27'.$text.'%27&From=%27'.$from.'%27&To=%27'.$to.'%27');
curl_setopt($ch, CURLOPT_USERPWD, $key.':'.$key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Parse the XML response
$result = curl_exec($ch);
$result = explode('<d:Text m:type="Edm.String">', $result);
$result = explode('</d:Text>', $result[1]);
$result = $result[0];
echo $result;