[英]Google translate API v2 encoding Chinese character and guide
Solution below as answer 下面的解决方案作为答案
$test = "橘色,灰色,深蓝,绿色";
$completeColours also contains the same text, just that the texts are joint by a for loop character by character to form the text like $test. $ completeColours还包含相同的文本,只是这些文本通过一个for循环字符逐个字符连接在一起,形成了像$ test这样的文本。
I copy pasted $test's charcters manually. 我手动复制粘贴的$ test的字符。 The weird problem here is, when I pass $test into the url below , it works!
奇怪的问题是,当我将$ test传递到下面的url时,它起作用了! BUT WHEN I put $completeColours instead of $test, it doesn't work :O
但是当我放$ completeColours而不是$ test时,它不起作用:O
$ch = curl_init('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/v1/Translate?To=%27'.$to.'%27&From=%27'.$from.'%27&Text=%27'.$test.'%27');
When i echo $test i just created it returns 橘色,ç°è‰²,æ·±è“,绿色
When i echo $completeColours it returns 橘色,灰色,深蓝,绿色
当我回显$ test时,我刚刚创建的它返回
橘色,ç°è‰²,æ·±è“,绿色
当我回显$ completeColours时,它返回橘色,灰色,深蓝,绿色
I have no clue, maybe try using the unicode? 我不知道,也许尝试使用unicode?
橘,色,灰色,深,蓝,绿色
results in 结果是
橘,色,灰色,深蓝,绿,色 橘,色,灰色,深蓝,绿,色
使用rawurlencode($completeColours)
代替。
Use google Translator API v2, it understands urlencode in chinese, Microsoft's translate api doesn't. 使用Google Translator API v2,它可以理解urlencode的中文版本,而Microsoft的Translation api则不然。
I will just share this code here, Google Translator API v2 using CURL in PHP 我将在此共享此代码,即在PHP中使用CURL的Google Translator API v2
$text = urlencode($completeColours);
$from = 'zh-CHS';
$to = 'en';
$ch = curl_init('https://www.googleapis.com/language/translate/v2?key=API_KEY_HERE&source=zh-CN&target=en&q='.$text.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$newresult = json_decode($result,true);
$student_id = $newresult['data']['translations'][0]['translatedText'];
echo $student_id;
If you are a first time user of google api, remember to set the quota in google API , your own set of quota, dont use the default. 如果您是google api的首次用户,请记住在google API中设置配额,您自己的配额集,不要使用默认值。 For some reason if you dont customize your own quota , it will return you a error Reached Max Limit.
由于某些原因,如果您不自定义自己的配额,它将返回一个错误的“达到最大限制”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.