Baidu Translation API PHP code, testing can be achieved, but English translation may need to convert the encoding.
<?phpfunction language ($value, $from = "Auto", $to = "Auto") { $value _code=urlencode ($value); #首先对要翻译的文字进行 urlencode processing $appid = "Your API Key"; #您注册的API Key $languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=". $appid. " &q= ". $value _code. "&from=". $from. " &to= ". $to; #生成翻译API的URL get address $text =json_decode (Language_text ($languageurl)); $text = $text->trans_result; return $text [0]->dst;} function Language_text ($url) #获取目标URL所打印的内容 { if (!function_exists (' file_get_contents ')) { $file _ Contents = file_get_contents ($url); } else { $ch = Curl_init (); $timeout = 5; curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $file _contents = curl_exec ($ch); Curl_close ($ch); } return $file _contents;} echo Language (' I love you '); >
PHP uses Baidu translation API sample sharing