8000 在线翻译接口 · Issue #41 · dalaolala/blog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
在线翻译接口 #41
Open
Open
@dalaolala

Description

@dalaolala
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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0