dtkl55257 2016-06-03 00:16
浏览 101
已采纳

Microsoft Translator api返回奇怪的字符

Maybe someone can help me with this. Using the Microsoft developer api for translation text will output for example this text in English , "This email address is already registered!" to French like this "Cette adresse email est déjà enregistrée !" which really should be like this: "Cette adresse email est déjà enregistrée!". Is there a way to fix this. My simple script is this in 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;
}
  • 写回答

1条回答 默认 最新

  • douzhouhan4618 2016-06-03 02:11
    关注

    Check this:

    PHP Curl UTF-8 Charset

    As it says there:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?