微软翻译器 api return 奇怪的字符
Microsoft Translator api return weird characters
也许有人可以帮我解决这个问题。使用 Microsoft 开发人员 api 翻译文本将输出例如英语文本,"This email address is already registered!" 到法语 "Cette adresse email est déjà enregistrée !",实际上应该是这样的:"Cette adresse email est déjà enregistrée!"。有没有办法来解决这个问题。我的简单脚本是 php:
public function translate($word, $from, $to)
{
//retrieve token
$access_token = $this->get_access_token();
$url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text='.urlencode($word).'&from='.$from.'&to='.$to;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:bearer '.$access_token,"Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False);
$rsp = curl_exec($ch);
$xmlObj = simplexml_load_string($rsp);
foreach((array)$xmlObj[0] as $val){
$translatedStr = $val;
}
return $translatedStr;
}
检查这个:
PHP Curl UTF-8 Charset
正如那里所说:
Simple: When you use curl it encodes the string to utf-8 you just need to decode them..
Description
string utf8_decode ( string $data )
This function decodes data , assumed to be UTF-8 encoded, to ISO-8859-1.
也许有人可以帮我解决这个问题。使用 Microsoft 开发人员 api 翻译文本将输出例如英语文本,"This email address is already registered!" 到法语 "Cette adresse email est déjà enregistrée !",实际上应该是这样的:"Cette adresse email est déjà enregistrée!"。有没有办法来解决这个问题。我的简单脚本是 php:
public function translate($word, $from, $to)
{
//retrieve token
$access_token = $this->get_access_token();
$url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?text='.urlencode($word).'&from='.$from.'&to='.$to;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization:bearer '.$access_token,"Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False);
$rsp = curl_exec($ch);
$xmlObj = simplexml_load_string($rsp);
foreach((array)$xmlObj[0] as $val){
$translatedStr = $val;
}
return $translatedStr;
}
检查这个:
PHP Curl UTF-8 Charset
正如那里所说:
Simple: When you use curl it encodes the string to utf-8 you just need to decode them..
Description
string utf8_decode ( string $data )
This function decodes data , assumed to be UTF-8 encoded, to ISO-8859-1.