日期:2014-05-16  浏览次数:20561 次

PHP调用有道词典翻译API实现翻译功能及代码
PHP调用有道词典翻译API实现翻译功能及代码,其中英文可以翻译,中文不能翻译是什么问题啊?急
http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=中文"
------解决方案--------------------
function getTransContent($url) {
     //   $url = "http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=" .urlencode($this->keyword);
     //  echo $url;
        // $url = urlencode($url);
        $value = file_get_contents($url);// echo $value;


       // $ch = curl_init();
        
      //  curl_setopt($ch, CURLOPT_URL, $url);
        
       // $html = curl_exec($ch);
   $ar = simplexml_load_string($value,'SimpleXMLElement',LIBXML_NOCDATA 
------解决方案--------------------
 LIBXML_NOBLANKS);
        //$jsonValue = json_decode ( $value, true );

$errorCode = $ar->errorCode;echo $errorCode.PHP_EOL;
        //翻译成功
      //  $errorCode = $jsonValue["erroeCode"];
        $trans = '';
        
        if (isset($errorCode)) {
         switch ($errorCode) {
         case 0:
         $trans = $ar->translation->paragraph;
         break;
         case 20:
         $trans = '要翻译的文本过长';
         break;
         case 30:
         $trans = '无法进行有效的翻译';
         break;
         case 40:
         $trans = '不支持的语言类型';
         break;
         case 50:
         $trans = '无效的key';
         break;
         default:
         $trans = '出现异常';
         break;        
         }
        }
        return $trans;
    }
$s=urlencode('中文');
echo getTransContent("http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=$s");

------解决方案