php调用谷歌翻译接口_PHP调用谷歌翻译

本文介绍了一个使用PHP调用谷歌翻译接口的示例,通过创建`Google_API_translator`类来设置翻译内容并进行翻译,实现从中文到英文的转换。示例中还提供了一个名为`gtranslate`的函数,可以直接翻译给定的文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public $text = "";//带翻译内容

public $out = "";//输出内容

public function setText($text)

{

$this->text = $text;

}

public function translate()

{

$this->out = "";

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

var_dump($gphtml);exit;

//提取翻译结果

$out = substr($gphtml,strpos($gphtml,''));

$out = substr($out,29);

$out = substr($out,0,strpos($out,''));

$this->out = $out;

return $this->out;

}

public function postPage($url,$text)

{

$html = '';

if($url != '' && $text != ''){

$ch = curl_init($url);

curl_setopt($ch,CURLOPT_REFERER,1);

curl_setopt($ch,CURLOPT_HEADER,1);

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);

curl_setopt($ch,CURLOPT_TIMEOUT,15);

$fields = array('hl=zh-CN', 'langpair=zh-CN|en', 'ie=UTF-8','text='.urlencode(mb_convert_encoding($text, 'UTF-8', 'GB2312')));

curl_setopt($ch,CURLOPT_POST,1);

curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $fields));

$html = curl_exec($ch);

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

curl_close($ch);

}

return $html;

}

}

//just for test

$g = new Google_API_translator();

$g->setText('我是谁!');

$g->translate();*/

function gtranslate($text,$to='zh-CN'){$entext = urlencode($text);$url = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=auto&tl='.$to.'&q='.$entext;set_time_limit(0);$ch =curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_MAXREDIRS,20);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40);

curl_setopt($ch, CURLOPT_URL, $url);$result = curl_exec($ch);

curl_close($ch);$result = json_decode($result);if(!empty($result)){foreach($result[0] as $k){$v[] = $k[0];

}return implode(" ", $v);

}

}$longtext = "Why Should You Get Our Smart Trainer for your Little Munchkin?

Upgraded Soft Waterproof Skin-Friendly Material: High-quality swimwear fabric and TPU film wrap which looks like leather but touches like skin. Fast dry and the bacteria are hard to breed on this foam baby swim float. Non-inflatable Air-Free Swim Trainer is the safest and durable float available in the market today

Upgraded Safety Buckles: The Upgraded safety buckle provides 5 support points and perfect 40° angle to keep the balance thus avoiding the baby to flip over and fall off. Upgraded into 2 swim ways for perfect Backstroke and Breaststroke. Backstroke Suitable for 3~12 months baby, Breaststroke Suitable for 3~24 months baby

Perfect Fit for Children: Suitable for babies weighing from 13 to 40 lbs. Suitable for Kids 3~24 months. Adjustable buckle at the back to suit most infant sizes. Easily adjustable with the attached Safety pin at the back of the float";echo gtranslate($longtext);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值