当前位置: 移动技术网 > IT编程>开发语言>PHP > Google翻译接口(PHP API)

Google翻译接口(PHP API)

2019年06月15日  | 移动技术网IT编程  | 我要评论

/* google翻译php接口

 * 官成文 2009-03-28

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

 * 注意:如果翻译文本为utf-8编码,则要删去mb_convert_encoding函数

 */

class google_api_translator {

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

public $text = “”;//翻译文本

public $out = “”; //翻译输出

function settext($text){

 $this->text = $text;

 }

function translate() {

 $this->out = “”;

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

//提取翻译结果

$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 – 界面语言,此处无用。

*langpair – src lang to dest lang

 *ie – urlencode的编码方式?

 *text – 要翻译的文本

*/

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网