首先需要先到http://fanyi.youdao.com/openapi?path=data-mode申请APIKEY。然后公布代码:
<?php
header("Content-Type: text/html;charset=utf-8");//解决中文乱码
function language($value){ $keyfrom = "*******"; //申请APIKEY时,填表的网站名称的内容 ;注意: $keyFrom 需要是【连续的英文、数字的组合】 $apikey = "********"; //从有道申请的APIKEY $qurl = 'http://fanyi.youdao.com/fanyiapi.do?keyfrom='.$keyfrom.'&key='.$apikey.'&type=data&doctype=json&version=1.1&q='.$value; $content = @file_get_contents($qurl); $sina = json_decode($content,true); $errorcode = $sina['errorCode']; $trans = ''; if(isset($errorcode)){ switch ($errorcode){ case 0: $trans = $sina['translation']['0']; break; case 20: $trans = '要翻译的文本过长'; break; case 30: $trans = '无法进行有效的翻译'; break; case 40: $trans = '不支持的语言类型'; break; case 50: $trans = '无效的key'; break; default: $trans = '出现异常'; break; } } return $trans; } echo language('世界你好');