Home >Backend Development >PHP Tutorial >php calls google online translation function_PHP tutorial

php calls google online translation function_PHP tutorial

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

We mainly use PHP to call the Google online translation function. Post a remote address and then use curl to read the Google translated value.

We mainly use PHP to call the Google online translation function. Post a remote address and then use curl to read the Google translated value and it will be OK.

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 api url of google translation    
    $gphtml = $this->postPage(array("url" => $google_translator_url));

            $this->out = $gphtml;

return $this->out; ";

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

$html = curl_exec($ch);

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

curl_close ($ch );

}

return $html;
}
}

$g = new Google_API_translator (); ();

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

Next, can you reverse translate 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; = "";


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);
}

return $html;

}

}

$g = new Google_API_translator();


$g-> ;translate();

A problem occurred, and a garbled code was returned. The strange thing is that this time I received English words sent from Google. How could there be an encoding error?
Is it that PHP’s curl cannot send unicode encoding or is there a problem with Google’s acceptance process? Copy PHP content to the clipboard PHP code: echo $google_translator_url;

The resulting url is copy PHP content to the clipboard PHP code: http://translate.google.com/translate_a/ t?client=t&text=%E8%AE%A1%E7%AE%97%E6%9C%BA&sl=zh_CN&tl=en


Directly enter this url into the address bar of the browser , without any problems (both IE and Firefox can pass).

Assuming that there is a problem with parsing the unicode during the transmission, I changed the URL of the api and changed "&sl=zh_CN&tl=en" to a completely wrong parameter. Copy the PHP content to the clipboard PHP code: http://translate.google.com/translate_a/t?client=t&text=%E8%AE%A1%E7%AE%97%E6%9C%BA&sl=en&tl=en

It’s strange that this time the php page can accept the Chinese string sent back by google, but it cannot display the result sent by google using the correct encoding parameters.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445056.htmlTechArticleWe mainly use PHP to call the Google online translation function, post a remote address and then use curl to read the Google translation The final value is OK. We mainly use php to call the google online translation function...
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