我来说一下这个东西的原理(大佬别看,我小声bb,说给那些萌新们听的)
说是关键词替换,但是我看完源代码,就是依靠的翻译。比如“再见”,翻译为英文“See you”,再翻译回来就成了“再见到你”,类似这样的原理,调用API实现的。这里放一下简单的代码:
[PHP] 纯文本查看 复制代码 <?php
function translate_google($text,$to='zh-CN'){
$entext = urlencode($text);
$url = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=auto&tl='.$to.'&q='.$entext;
set_time_limit(0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS,20);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
if(!empty($result)){
foreach($result[0] as $k){
$v[] = $k[0];
}
return implode(" ", $v);
}
}
$wyc = "需要伪原创的字符";
echo translate_google(translate_google($wyc,'en')); |