Open
Description
function geturl($url){
$headerArray =array("Content-type:application/json;","Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}
function translate($word, $en = 'en')
{
if( empty($word) ) return '';
$word = htmlspecialchars($word, ENT_NOQUOTES);
$len = mb_strlen($word, 'UTF-8');
$l = 5000; #单次最大翻译内容长度
$url = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=auto&tl='.$en.'&q=';
$text = '';
for($i = 0; $i < $len; $i += $l)
{
$fanyi = geturl($url.urlencode( mb_substr( $word, $i, $l, "UTF-8" ) ));
echo $url.urlencode( mb_substr( $word, $i, $l, "UTF-8" ) );
if(!empty($fanyi['content']))
{
$json = json_decode($fanyi['content'], true);
if(isset($json[0][0][0]) && !empty($json[0][0][0]))$text .= $json[0][0][0];
}
}
return $text;
}
Metadata
Metadata
Assignees
Labels
No labels