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.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料