3

免费谷歌翻译api

 2 years ago
source link: https://www.80shihua.com/archives/2339
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

免费谷歌翻译api

作者: dreamfly 分类: php 发布时间: 2020-04-22 23:21

我们知道对于国际语言的翻译,谷歌做的还是很优秀的,但是因为某些原因,国内无法使用谷歌云来进行翻译,所以我们需要一种可以通过不需要授权请求api的方式,这个时候,php-google-sdk可以方便我们实现这个功能。

安装

composer require stichoza/google-translate-php

引入class


use Stichoza\GoogleTranslate\TranslateClient;

创建请求对象

$tr=new TranslateClient();

设置国内请求url

$tr->setUrlBase('http://translate.google.cn/translate_a/single');

设置翻译源语言,目的语言

$tr->setSource('en'); 
$tr->setTarget('ka');

翻译句子

echo $tr->translate('Hello World!');

链式操作

echo $tr->setSource('en')->setTarget('ka')->translate('Goodbye');

设置代理

$tr->setHttpOption(['proxy'=>'tcp://localhost:8090']);

批量翻译

echo $tr->translate(['I can dance', 'I like trains', 'Double rainbow']);

注意事项
此函数库只供学习交流使用,如果频繁请求,可能会导致谷歌封禁ip。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK