关于谷歌翻译接口调用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,去调试。
然后设置好
源语言:中文(简体)
目标语言:英文
然后再随便输入一串汉字,比如:
他们是这样说的
然后可以看到结果为:
其中,此处是自动实时翻译,所以没有点击翻译按钮,就可以看到翻译的结果:
They say
了。
2. 然后接下来去找,到底此翻译的请求,是哪个地址。
而对于IE9的F12中,抓到很多url,内容繁多,也不容易找到到底哪个是我们想要的。
但是我们现在已经知道了翻译结果为:
They say
所以,就可以通过F12自带的搜索功能,去找到对应的url了:
如上,再点击Back to summary view,就可以看到是这个地址了:
对应的地址,在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. 由此信息,便可以去写代码了。
经过一番代码调试,最后搞定:

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

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

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

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.

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

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment
