Home > Backend Development > PHP Tutorial > Google Translate Interface (PHP API)_PHP Tutorial

Google Translate Interface (PHP API)_PHP Tutorial

WBOY
Release: 2016-07-13 17:43:38
Original
970 people have browsed it

/* Google Translate PHP interface

* Official document 2009-03-28

* http://blog.csdn.net/aprin/

* Note: If the translated text is UTF-8 encoded, the mb_convert_encoding function must be deleted

*/

class Google_API_translator {

public $url = “http://translate.google.com/translate_t”;

public $text = “”;//Translation text

public $out = “”; //Translation output

function setText($text){

$this->text = $text;

}

function translate() {

$this->out = “”;

$gphtml = $this->postPage($this->url, $this->text);

//Extract translation results

$out = substr($gphtml, strpos($gphtml, “

“));

$out = substr($out, 29);

$out = substr($out, 0, strpos($out, “

));

$this->out = $out;

return $this->out;

}

function postPage($url, $text) {

$html =”;

if($url != “” && $text != “”) {

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, 15);

/*

*hl – interface language, useless here.

*langpair – src lang to dest lang

*ie – What is the encoding method of urlencode?

*text – the text to be translated

*/

$fields = array(‘hl=zh-CN&rs

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478828.htmlTechArticle/* Google Translate PHP Interface* Official Document 2009-03-28 * http://blog.csdn.net /aprin/ * Note: If the translated text is UTF-8 encoded, the mb_convert_encoding function must be deleted */ class Google_AP...

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template