ThinkPHP calls Baidu translation class for online translation

Source: Internet
Author: User

To implement the ThinkPHP online translation function, you must register an API key in the Baidu Developer Center. The specific implementation code is as follows:

Action Code:

<? 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 "; // enter the API key $ languageurl = "http: // o Penapi.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 implements age_text ($ url) // obtain the content printed by the target 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:

<Div id = "content"> <div style = "padding: 5px; "> <form action =" _ APP _/fanyi/"method =" POST "name =" fanyi "> <p> original article: </p> <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 =" Translation "style =" text-align: center; "> <input type =" reset "onclick =" reset () "class =" search-submit "value =" blank "style =" text-align: center; "> </form> <p> Translation: </p> <textarea name = "rs" id = "yiwen" rows = "10" cols = "80" style = "padding: 5px; width: 661px; height: 217px; ">{$ rs }</textarea> </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.