欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Google翻译接口(PHP API)

程序员文章站 2024-01-06 14:06:34
...

/* Google翻译PHP接口

* 官成文 2009-03-28

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

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

*/

class Google_API_translator {

public $url = “http://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

Google翻译接口(PHP API)

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

上一篇:

下一篇: