Home  >  Article  >  Backend Development  >  PHP calls google online translation function_PHP tutorial

PHP calls google online translation function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:06:221501browse

php calls the google online translation function

from Chinese to English

class Google_API_translator {


public $out = "";

function translate() {
$this->out = "";
$text = urlencode("Computer");//The word to be translated

$google_translator_url = "http://translate.google.com /translate_a/t?client=t&text=".$text."&sl=zh_CN&tl=en";

echo $google_translator_url;
$gphtml = $this->postPage(array("url" => $google_translator_url));

$this->out = $gphtml;

return $this->out; ) {
$html = "";

if($opts["url"] != "") {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $opts["url"]);

     $html = curl_exec($ch);

if(curl_errno($ch)) $html = " ";

            curl_close ($ch);

$g->translate();

From English to Chinese



class Google_API_translator {

public $out = "";

function translate() {

$this->out = ""; $text = urlencode("computer");//The word to be translated

       $google_translator_url = "http://translate.google.com/translate_a/t?client=t&text=".$text."&sl=en&tl=zh_CN";

//Piece together the google translation api url                                                                                                       $this-> $this->out;

}
function postPage($opts) {

  $html = "";


if($opts["url"] != "") {
           $ch = curl_init(); >

if(curl_errno($ch)) $html = "";

      curl_close ($ch);

     }

return $html; >}

$g = new Google_API_translator();


$g->translate();

It’s perfect, there are no encoding issues in the returned results. The Chinese encoding returned from Google is completely correct.



http://www.bkjia.com/PHPjc/445043.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/445043.html

TechArticle

php calls the google online translation function from Chinese to English class Google_API_translator { public $out = ; function translate() { $ this-out = ; $text = urlencode(computer);//The word to be translated...

Statement:
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