Home >Backend Development >PHP Tutorial >php calls google online translation function_PHP tutorial
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.
$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)) ;
if($opts["url"] != "") {
curl_setopt($ch, CURLOPT_URL, $opts["url "]);
$html = curl_exec($ch);
curl_close ( $ch);
}
}
}$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;
Directly enter this url into the address bar of the browser , without any problems (both IE and Firefox can pass).