search
HomeBackend DevelopmentPHP Tutorial关于谷歌翻译接口调用php步骤

关于谷歌翻译接口调用php方法

这两天在整数据库,发现里面好多数据翻译不准确,那么问题来了,从哪去找接口来翻译数据,请不起人,只有靠网上的翻译了,开始的时候发现谷歌以前的api翻译接口貌似收费了。。。我是个穷人,用不起哭,后来发现百度的可以用,就用了一下:

下面是代码:

$url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=YourApiKey&q=today&from=auto&to=auto";

ob_start(); 
readfile($url);
$result = ob_get_contents(); 
ob_end_clean();

后来又发现百度翻译。。真的不准。。。又想用谷歌,所以在网上找了很多资料,发现还是有办法用的:

代码如下:

function getname($name) { 
        $urlqueue = "";
$contents = "";
$titles = "";
$str = "http://translate.google.cn/translate_a/t?client=t&text=".$name."&hl=zh-CN&sl=en&tl=zh-CN&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&it=srcd_gms.1378&ssel=4&tsel=6&sc=1";
ob_start(); 
readfile($str);
$result = ob_get_contents(); 
ob_end_clean();
preg_match_all('/\[\[\[\"([\s\S]*?)\"/',$result,$match);

//$arr=json_decode($result,true);
$namenew = $match[1][0];
return $namenew;
}


在线面试我借鉴的资料大家可以看看。。。。


今日,在想要在C#中,利用google翻译,实现把中文翻译为英文。

然后就去找,对应翻译的url和参数是如何使用的。

参考之前自己的的BlogsToWordpress中的Python版本的crifanLib:crifanLib.py中的translateString,然后去写代码,结果竟然发现,无法成功获得对应的返回的html,因此也就无法找到翻译后的结果了。

所以,就打算手动自己去找当前可用的google翻译。


1.找到网页版的google翻译,首地址是:    

http://translate.google.cn/

然后使用之前在:

【总结】浏览器中的开发人员工具(IE9的F12和Chrome的Ctrl+Shift+I)-网页分析的利器

中所介绍的IE9的F12,去调试。

然后设置好

源语言:中文(简体)

目标语言:英文

然后再随便输入一串汉字,比如:

他们是这样说的

然后可以看到结果为:

tamenshizheyangshuode

其中,此处是自动实时翻译,所以没有点击翻译按钮,就可以看到翻译的结果:

They say

了。

2. 然后接下来去找,到底此翻译的请求,是哪个地址。

而对于IE9的F12中,抓到很多url,内容繁多,也不容易找到到底哪个是我们想要的。

但是我们现在已经知道了翻译结果为:

They say

所以,就可以通过F12自带的搜索功能,去找到对应的url了:

click back to summary view

如上,再点击Back to summary view,就可以看到是这个地址了:

find this url

对应的地址,在Detail view中,拷贝出来就是:

http://translate.google.cn/translate_a/t?client=t&text=%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84&hl=zh-CN&sl=zh-CN&tl=en&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&it=srcd_gms.1378&ssel=4&tsel=6&sc=1

然后将其分解为:

http://translate.google.cn/translate_a/t?    
client=t&    
text=%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84&    
hl=zh-CN&    
sl=zh-CN&    
tl=en&    
ie=UTF-8&    
oe=UTF-8&    
multires=1&    
otf=1&    
pc=1&    
it=srcd_gms.1378&    
ssel=4&    
tsel=6&    
sc=1

3. 由此信息,便可以去写代码了。

经过一番代码调试,最后搞定:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//-----------------------------------------------------------------------------
Statement
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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment