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

php中文网
发布: 2016-07-13 10:26:14
原创
1275人浏览过

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

action代码部分:

<&#63;php
class 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";//这里填写你在百度上申请的API key  
$languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate&#63;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;
}
}
&#63;>
登录后复制

html代码部分:

<div id="content">
<div style="padding:5px;">
<form action="__APP__/fanyi/" method="POST" name="fanyi">
<p>原文:</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/950">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680003823895.png" alt="ViiTor实时翻译">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/950">ViiTor实时翻译</a>
                            <p>AI实时多语言翻译专家!强大的语音识别、AR翻译功能。</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="ViiTor实时翻译">
                                <span>116</span>
                            </div>
                        </div>
                        <a href="/ai/950" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="ViiTor实时翻译">
                        </a>
                    </div>
                
<textarea name="value" id="yuanwen" rows="10" cols="80" style="padding:5px;width: 661px; height: 217px;">{$value}</textarea>
<input type="submit" class="search-submit" value="翻 译" style="text-align:center;">
    
<input type="reset" onclick="reset()" class="search-submit" value="清 空" style="text-align:center;">
</form>
<p>译文:</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<textarea name="rs" id="yiwen" rows="10" cols="80" style="padding:5px;width: 661px; height: 217px;">{$rs}</textarea> </div>
</div>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/824771.htmlTechArticle要实现thinkphp在线翻译功能,需要先在百度开发者中心注册一个api key,具体实现代码如下: action代码部分: phpclass FanyiAction extends Action {...
相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号