thinkphp call Baidu translation class to achieve online translator _php example

Source: Internet
Author: User

To achieve thinkphp online translation function, you need to first register an API key in the Baidu Developer Center, the specific implementation code is as follows:

Action Code section:

<?php class Fanyiaction extends Action {public Function _empty () {header (' http/1.0 404 Not Found '); 
  $this->display ("public:404"); 
The 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"; Fill in your application on 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); The Public Function Language_text ($url)//Gets the content printed by the destination URL {if (!function_exists (' file_get_contents ')) {$file _contents = f Ile_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:

<div id= "Content" >
<div style= "padding:5px;" >
<form action= "__app__/fanyi/" method= "POST" name= "Fanyi" >
<p> original:</p>
< TextArea name= "value" id= "Yuanwen" rows= "" cols= "" style= "padding:5px;width:661px; height:217px; " >{$value}</textarea> <input type= "Submit" class= "Search-submit" "value="
translation "style=" Text-align: Center; " >
<input type= "reset" onclick= "Reset ()" class= "Search-submit" value= "Empty" style= "Text-align:center" >
</form>
<p> translation:</p>
<textarea name= "rs" id= "Yiwen" rows= "" cols= "" "style=" "PADDING:5PX;WIDTH:661PX; height:217px; " >{$rs}</textarea> </div>
</div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.