Home > php教程 > php手册 > ThinkPHP调用百度翻译类实现在线翻译

ThinkPHP调用百度翻译类实现在线翻译

WBOY
Release: 2016-06-06 20:20:58
Original
2009 people have browsed it

这篇文章主要介绍了ThinkPHP调用百度翻译类实现在线翻译,需要的朋友可以参考下

要实现ThinkPHP在线翻译功能,需要先在百度开发者中心注册一个API key,,具体实现代码如下:

action代码部分:

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";//这里填写你在百度上申请的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) //获取目标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代码部分:

原文:

    

译文:

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template