想用来作为火车头采集器的翻译插件,但是用不起来。
以下这段代码来自这里
GoogleTranslate-基于Google翻译的免费火车头PHP翻译插件 | 许多多
https://www.xuduoduo.com/locoy-GoogleTranslate.html
请懂火车头采集器的朋友出手帮忙哈!
<?php
function translate($text){
$query = urlencode($text);
//$url = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl='.FROM.'&tl='.TO.'&q='.$query; 这是作者的版本,我看google的接口方式变了,就改成下面这样了,其他没改一个代码~
$url = 'https://translate.google.cn/?sl='.FROM.'&tl='.TO.'&client=gtx&dt=t&ie=UTF-8&oe=UTF-8&op=translate&text='.$query;
set_time_limit(0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_REFERER, "https://translate.google.cn" );
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36' );
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);
}
}
//以下是关于火车头采集器的相关设置
if($LabelArray['PageType']=="Save")
{
$direction = explode('>' , $LabelArray['翻译方向']);
define("FROM",$direction[0]);
define("TO",$direction[1]);
$tab = explode(',' , $LabelArray['翻译标签']);
foreach($tab as $t_value){
$arr[] = explode('>' , $t_value);
}
$arrlength=count($arr);
for($x=0;$x<$arrlength;$x++){
$LabelArray[''.$arr[$x][1].''] = translate($LabelArray[''.$arr[$x][0].'']);
}
}
echo serialize($LabelArray);
?>