To implement the Thinkphp online translator function, you need to first register an API key in the Baidu Developer Center, the implementation code is as follows:
Action Code section:
<?phpclass Fanyiaction extends Action {public Function _empty () {header (' http/1.0 404 Not Found '); $this->display ("public:404"); }public function Index () {$User = A ("index"); $User->head (); $User->right (); $User->footer (); $this Display ();} Public Function Fanyi () {$value =$_post[' value '); $from = "Auto"; $to = "Auto"; $value _code=urlencode ($value); $appid = " XXXXXXXX ";//here fill in your application on the Baidu API Key $languageurl =" http://openapi.baidu.com/public/2.0/bmt/translate?client_id= ". $appid. " &q= ". $value _code. "&from=". $from. " &to= ". $to; $text =json_decode ($this->language_text ($languageurl)); $text = $text->trans_result; $rs = $text [0]->dst; $this->assign (' value ', $value), $this->assign (' rs ', $rs); $User = A ("Index"); $User->head (); $User->right (); $User->footer (); $this->display (index);} Public Function Language_text ($url)//Gets the content printed by the destination 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;}}? >
HTML code section:
President
{$rs}
http://www.bkjia.com/PHPjc/824771.html www.bkjia.com true http://www.bkjia.com/PHPjc/824771.html techarticle to implement the thinkphp online translator function, you need to first register an API key in the Baidu Developer Center, the implementation code is as follows: Action Code section: Phpclass Fanyiaction extends Action {...