nqkk58 发表于 2024-10-4 18:19:00

PHP是怎么样调用设备翻译API的—以搜狗翻译为例


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">前言</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在《译者编程入门指南》的最后一章<span style="color: black;">咱们</span>介绍了<span style="color: black;">怎样</span>调用百度翻译和有道翻译的API,<span style="color: black;">然则</span>在这本入门教程中,<span style="color: black;">咱们</span>仅仅是教会了<span style="color: black;">大众</span><span style="color: black;">怎样</span>将官网上的PHP代码复制下来修改,并<span style="color: black;">无</span><span style="color: black;">仔细</span>介绍原理。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,<span style="color: black;">咱们</span>以调用搜狗翻译为例,为<span style="color: black;">大众</span>介绍PHP是<span style="color: black;">怎样</span>调用<span style="color: black;">设备</span>翻译API的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">正文</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1、</span>搜狗翻译的API文档</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>想调用搜狗翻译的API,需要先前往搜狗深智引擎的官方网站:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://deepi.sogou.com/</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/62QsgCMxJxcEEKe50jmvsBClXSXGlU62q9z72IqOxZOiazYGXbdUkw91t0qDZXwXukzT6icHHcacDF1f4kzqD9tQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">不得不说,<span style="color: black;">倘若</span>不仔细看还以为搜狗<span style="color: black;">设备</span>翻译的logo是DeepL,<span style="color: black;">实质</span>上是DeepI。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">进入后前往“文档中心”-“文本翻译”-“接入文档”:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/62QsgCMxJxcEEKe50jmvsBClXSXGlU62xCkj5jVqic12UKbeqpq1trII9viclS7QsOiatpicXTicDFtZTian6So3HHPQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">这儿</span>能看到非常<span style="color: black;">详细</span>的接入<span style="color: black;">办法</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">然而在“各语言接入DEMO”部分<span style="color: black;">咱们</span>并<span style="color: black;">无</span>看到PHP代码,<span style="color: black;">仅有</span>Python、Java、C#。显然它大意了,但<span style="color: black;">咱们</span><span style="color: black;">不可</span>闪,<span style="color: black;">咱们</span>要把用PHP调用的代码给写出来。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、</span>搜狗文档中心<span style="color: black;">供给</span>的接入向导</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1)接入<span style="color: black;">位置</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">想要</span>调用搜狗翻译的API,就得<span style="color: black;">晓得</span>它的<span style="color: black;">位置</span>是什么。在其文档中心有<span style="color: black;">这般</span>一段话:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/62QsgCMxJxcEEKe50jmvsBClXSXGlU62x7qgNvU16jSC2HHibeb5wHmILCsleSg0yvCwU2XqGpvia2mCsO8w9rvw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>面有几个关键信息:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTTP<span style="color: black;">位置</span></p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">POST<span style="color: black;">办法</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">字段</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">什么意思呢?</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这就要给<span style="color: black;">大众</span>讲讲什么是HTTP协议了。<span style="color: black;">仔细</span>内容<span style="color: black;">亦</span>可参考菜鸟教程<span style="color: black;">供给</span>的HTTP教程:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://www.runoob.com/http/http-tutorial.html</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简单<span style="color: black;">来讲</span>,<span style="color: black;">咱们</span>平时上网输入网址时习惯在网址前面加上http://,<span style="color: black;">而后</span>才是输入域名。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个http<span style="color: black;">便是</span>HTTP协议,全<span style="color: black;">叫作</span>是:HyperText Transfer Protocol,超文本传输协议。从字面上来看,这是一个规定<span style="color: black;">怎样</span>传输数据的协议。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>平时会<span style="color: black;">运用</span>浏览器来<span style="color: black;">拜访</span>某个网站,当<span style="color: black;">咱们</span>打开浏览器的时候就相当于打开了一个遵从HTTP协议的客户端,接下来<span style="color: black;">咱们</span>输入网址再按回车,<span style="color: black;">实质</span>上<span style="color: black;">便是</span>给浏览器发送一个指令:去吧,比卡丘,去http://www.miaoxing.com给我抓只喵回来!</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个时候比卡丘就会读取这个网址,解析<span style="color: black;">背面</span>的真实服务器的<span style="color: black;">位置</span>,这个<span style="color: black;">所说</span>的服务器<span style="color: black;">便是</span>一台真实的电脑,这台电脑里有数据,比卡丘会把数据拿到后再传回到浏览器,<span style="color: black;">最后</span><span style="color: black;">表示</span>在浏览器中。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上<span style="color: black;">便是</span><span style="color: black;">咱们</span><span style="color: black;">经过</span>浏览器看网页的过程。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">这般</span>说<span style="color: black;">亦</span>不<span style="color: black;">详细</span>,<span style="color: black;">咱们</span>来<span style="color: black;">瞧瞧</span>当<span style="color: black;">咱们</span><span style="color: black;">拜访</span>一个网址时,浏览器发送出去的<span style="color: black;">到底</span>是什么指令吧:</p><span style="color: black;"><span style="color: black;">GET</span> <span style="color: black;">/hello.txt</span> HTTP/1.1</span><span style="color: black;"><span style="color: black;">User-Agent</span>: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3</span><span style="color: black;"><span style="color: black;">Host:&nbsp;www.miaoxing.com</span></span><span style="color: black;"><span style="color: black;">Accept-Language</span>: en, mi</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">你<span style="color: black;">瞧瞧</span>,这<span style="color: black;">实质</span>上<span style="color: black;">便是</span>几行非常简易的代码,最顶上的<span style="color: black;">第1</span>个单词GET<span style="color: black;">显示</span>这个浏览器是用GET的<span style="color: black;">办法</span>将后面的指令发送出去的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GET是一种浏览器向服务器索要数据的一种<span style="color: black;">办法</span>,其他的<span style="color: black;">办法</span>还有POST、PUT、DELETE等好几种,最常用的<span style="color: black;">便是</span>GET和POST。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在搜狗翻译的API中它说要“用POST<span style="color: black;">办法</span>发送数据”,这<span style="color: black;">便是</span>浏览器索要数据时要用的<span style="color: black;">办法</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">浏览器的索取数据命令发出去之后服务器会返回什么数据给浏览器呢?如下图:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/62QsgCMxJxcEEKe50jmvsBClXSXGlU62iaprZE4PICYKaZicnEXSMAzq1SU7j318BXp2etsp2tNoA1x2guJibgImQ/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">菜鸟教程中<span style="color: black;">供给</span>的这幅图就很直观了,最顶上是状态行,<span style="color: black;">倘若</span>状态码是200,<span style="color: black;">那样</span>就<span style="color: black;">显示</span>服务器成功返回了数据,<span style="color: black;">倘若</span>是404,就<span style="color: black;">显示</span>服务器上<span style="color: black;">无</span>浏览器索要的数据,<span style="color: black;">通常</span>这种<span style="color: black;">状况</span><span style="color: black;">便是</span>数据被删掉了、被和谐掉了。这<span style="color: black;">亦</span>是网友们玩儿404梗的源泉。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">状态行后面还有<span style="color: black;">信息</span>报头和响应正文,<span style="color: black;">这儿</span>的响应正文<span style="color: black;">大众</span>一看就<span style="color: black;">晓得</span>是啥,这不<span style="color: black;">便是</span>HTML网页代码吗?是的,这个正文返回给浏览器后,浏览器一渲染就把好看的页面展示给<span style="color: black;">大众</span>了。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不外</span><span style="color: black;">大众</span>还得再回去<span style="color: black;">瞧瞧</span>那个<span style="color: black;">信息</span>报头,里面有个Content-Type,这玩意儿决定了浏览器用什么形式、什么编码来读取服务器返回的数据的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>平时<span style="color: black;">拜访</span>网站时得到的数据<span style="color: black;">常常</span>有HTML网页、纯文本、<span style="color: black;">照片</span>等,<span style="color: black;">她们</span>对应的代码<span style="color: black;">通常</span>是<span style="color: black;">这般</span>的:</p>
    <span style="color: black;">text/html :HTML格式</span><span style="color: black;">text/plain :纯文本格式</span><span style="color: black;">text/xml :XML格式</span><span style="color: black;">image/gif :gif<span style="color: black;">照片</span>格式</span><span style="color: black;">image/jpeg :jpg<span style="color: black;">照片</span>格式</span><span style="color: black;">image/png:png<span style="color: black;">照片</span>格式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>还有一类是<span style="color: black;">这般</span>的:</p>
    <span style="color: black;">application/pdf:pdf格式</span><span style="color: black;">application/msword :Word文档格式</span><span style="color: black;">application/x-www-form-urlencoded</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前面的pdf和msword<span style="color: black;">大众</span>一看就<span style="color: black;">晓得</span>是什么,但最后面这个x-www-form-urlencoded肯定<span style="color: black;">无</span>几个人见过。这种<span style="color: black;">通常</span>是<span style="color: black;">咱们</span>表单(form)传输出去的数据,<span style="color: black;">例如</span>我有一个表单是<span style="color: black;">这般</span>的:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">姓名:________</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">秘码</span>:________</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span><span style="color: black;">咱们</span>会用name和password来分别<span style="color: black;">暗示</span>姓名、<span style="color: black;">秘码</span>,<span style="color: black;">而后</span>把你输入的姓名和<span style="color: black;">秘码</span>转换成<span style="color: black;">这般</span>:</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">name=hanlintao&amp;password=bugaosuni</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">注:上面的连接符用的是&amp;,读作ampersand,<span style="color: black;">暗示</span>“和”的意思</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span>服务器一看就<span style="color: black;">晓得</span>姓名是什么,<span style="color: black;">秘码</span>是什么。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>,上面这一堆文字<span style="color: black;">便是</span>想告诉<span style="color: black;">大众</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为何</span>搜狗翻译<span style="color: black;">期盼</span><span style="color: black;">咱们</span>调用它的时候要用Content-Type:application/x-www-form-urlencoded呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>搜狗翻译<span style="color: black;">期盼</span>传递给它的数据用name=hanlintao&amp;password=bugaosuni这种方式呈现给它。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">你不是想让搜狗翻译帮你翻译一句中文吗?那就<span style="color: black;">这般</span>告诉它:source=北京语言大学&amp;yuanwen=zhongwen&amp;yiwen=yingwen</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是不是一目了然?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>搜狗翻译要是翻完了发给你,应该怎么发呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">答案是:Accept:application/json</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span><span style="color: black;">便是</span>搜狗翻译用JSON这种形式来返回数据给你。啥是JSON呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实搜狗翻译在文档中给出了示例:</p><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">"zly"</span>: <span style="color: black;">"zly"</span>,</span><span style="color: black;"> <span style="color: black;">"query"</span>: <span style="color: black;">"good"</span>, <span style="color: black;">//<span style="color: black;">查找</span>词</span></span><span style="color: black;"> <span style="color: black;">"translation"</span>: <span style="color: black;">"好的"</span>, <span style="color: black;">//<span style="color: black;">设备</span>翻译结果,<span style="color: black;">查找</span>正确时,<span style="color: black;">必定</span>会有结果</span></span><span style="color: black;"> <span style="color: black;">"errorCode"</span>: <span style="color: black;">"0"</span>, <span style="color: black;">//<span style="color: black;">查找</span>结果状态,<span style="color: black;">详细</span><span style="color: black;">道理</span>可参考“错误码列表”</span></span><span style="color: black;"> <span style="color: black;">"detect"</span>: <span style="color: black;">"en"</span> <span style="color: black;">//语种检测结果,代码所<span style="color: black;">表率</span>的语言请查看“语种列表”</span></span><span style="color: black;">}</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这<span style="color: black;">便是</span>JSON格式的数据,里面啥都<span style="color: black;">包括</span>进去了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">讲了这么多<span style="color: black;">便是</span>为<span style="color: black;">认识</span>释下面这个截图:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/62QsgCMxJxcEEKe50jmvsBClXSXGlU62x7qgNvU16jSC2HHibeb5wHmILCsleSg0yvCwU2XqGpvia2mCsO8w9rvw/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span><span style="color: black;">咱们</span>在写代码时要<span style="color: black;">根据</span>上图的提示来写,但<span style="color: black;">详细</span><span style="color: black;">怎样</span>写,我后面再说明。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2)调用参数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/sz_mmbiz_png/62QsgCMxJxcEEKe50jmvsBClXSXGlU62U5mknmInR1BT46zvtrdibrkSOM7s8XwRnY7Cz6rNicscDzhPzQvGoEBA/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在上面一步<span style="color: black;">咱们</span><span style="color: black;">晓得</span>搜狗<span style="color: black;">期盼</span><span style="color: black;">咱们</span>将数据以name=hanlintao&amp;password=bugaosuni这种方式发送给它,<span style="color: black;">然则</span><span style="color: black;">详细</span>要发送什么数据给它并<span style="color: black;">无</span>说,于是在第二部分的调用参数表格中它就说得很清楚了。<span style="color: black;">这儿</span>面有几个必填的参数,分别是:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">q、from、to、pid、salt、sign</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">显然,<span style="color: black;">咱们</span>还是比较容易将q、from、to获取到的,这不<span style="color: black;">便是</span>待翻译的原文、源语言和<span style="color: black;">目的</span>语言吗?pid<span style="color: black;">亦</span>比较容易,注册了之后就<span style="color: black;">能够</span><span style="color: black;">得到</span>,这个<span style="color: black;">便是</span>用来识别你是谁的,是注册完搜狗翻译API后分配给你个人的<span style="color: black;">独一</span>ID。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">salt是什么?有些人会直接发这个salt翻译成“盐”,<span style="color: black;">实质</span>上这是<span style="color: black;">秘码</span>学里的一个术语,<span style="color: black;">这儿</span>就先不解释了,这<span style="color: black;">便是</span>个随机数字,<span style="color: black;">能够</span>自动生成。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sign是签名的意思,<span style="color: black;">实质</span>上<span style="color: black;">便是</span>前面的pid和q、salt以及用户<span style="color: black;">秘码</span><span style="color: black;">构成</span>的集合的加密结果,加密的方式是用md5<span style="color: black;">办法</span>,md5是一种非常通用的加密算法,加密容易破解难。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了以上这些参数,<span style="color: black;">咱们</span>就<span style="color: black;">能够</span><span style="color: black;">起始</span>写代码了。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3)模拟浏览器发送数据的cURL工具</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">这儿</span>要用的PHP工具是cURL。<span style="color: black;">能够</span>理解为这个工具<span style="color: black;">便是</span>模拟浏览器的<span style="color: black;">拜访</span>。<span style="color: black;">由于</span><span style="color: black;">咱们</span>既然是写代码来调用<span style="color: black;">设备</span>翻译API,肯定<span style="color: black;">不可</span>直接打开浏览器去<span style="color: black;">拜访</span>前面那个HTTP<span style="color: black;">位置</span>,<span style="color: black;">因此呢</span><span style="color: black;">咱们</span>就需要用PHP的模拟浏览器来发送数据和接收数据,这个模拟浏览器<span style="color: black;">便是</span>cURL。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cURL中的“c”<span style="color: black;">便是</span>Client的意思,“URL”是网址,<span style="color: black;">因此</span><span style="color: black;">咱们</span>是用代码来模拟<span style="color: black;">拜访</span>某个网址的浏览器客户端。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个工具的<span style="color: black;">运用</span>基本<span style="color: black;">办法</span>如下:</p><span style="color: black;"><span style="color: black;"><span style="color: black;">&lt;?php</span></span></span><span style="color: black;"><span style="color: black;">//创建一个“浏览器”</span></span><span style="color: black;">$ch = curl_init();</span><span style="color: black;"><span style="color: black;">//设置要发送的数据</span></span><span style="color: black;">$q = <span style="color: black;">"北京语言大学"</span>;</span><span style="color: black;">$from = <span style="color: black;">"zh-CHS"</span>;</span><span style="color: black;">$to = <span style="color: black;">"en"</span>;</span><span style="color: black;">$pid = <span style="color: black;">""</span>;</span><span style="color: black;">$key = <span style="color: black;">""</span>;</span><span style="color: black;">$salt = rand(<span style="color: black;">1</span>,<span style="color: black;">100</span>);</span><span style="color: black;">$sign = md5($pid.$q.$salt.$key);</span><span style="color: black;"><span style="color: black;">//将数据拼接成name=hanlintao&amp;password=bugaosuni形式</span></span><span style="color: black;">$data = <span style="color: black;">"q="</span>.$q.<span style="color: black;">"&amp;from="</span>.$from.<span style="color: black;">"&amp;to="</span>.$to.<span style="color: black;">"&amp;pid="</span>.$pid.<span style="color: black;">"&amp;salt="</span>.$salt.<span style="color: black;">"&amp;sign="</span>.$sign;</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”要<span style="color: black;">拜访</span>哪个链接</span></span><span style="color: black;">curl_setopt($ch, CURLOPT_URL, <span style="color: black;">"http://fanyi.sogou.com/reventondc/api/sogouTranslate"</span>);</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”发送和接收的数据格式</span></span><span style="color: black;">curl_setopt($ch, CURLOPT_HTTPHEADER,<span style="color: black;">array</span>(<span style="color: black;">"Content-Type:application/x-www-form-urlencoded"</span>,<span style="color: black;">"Accept:application/json"</span>));</span><span style="color: black;">curl_setopt($ch, CURLOPT_RETURNTRANSFER,<span style="color: black;">1</span>);</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”用POST<span style="color: black;">办法</span>来发送拼接好的数据</span></span><span style="color: black;">curl_setopt($ch, CURLOPT_POSTFIELDS, $data);</span><span style="color: black;">curl_setopt($ch, CURLOPT_POST, <span style="color: black;">1</span>);</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”把<span style="color: black;">拜访</span><span style="color: black;">目的</span>网址后得到的结果放在$output变量中</span></span><span style="color: black;">$output = curl_exec($ch);</span><span style="color: black;"><span style="color: black;">//打印结果</span></span><span style="color: black;">print_r($output);</span><span style="color: black;"><span style="color: black;">//关闭“浏览器”</span></span><span style="color: black;">curl_close($ch);</span><span style="color: black;"><span style="color: black;">?&gt;</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上</span>代码运行完之后的结果是:</p>
    <span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">"zly"</span>:<span style="color: black;">"zly"</span>,</span><span style="color: black;"> <span style="color: black;">"query"</span>:<span style="color: black;">"北京语言大学"</span>,</span><span style="color: black;"> <span style="color: black;">"translation"</span>:<span style="color: black;">"Beijing Language and Culture University"</span>,</span><span style="color: black;"> <span style="color: black;">"errorCode"</span>:<span style="color: black;">"0"</span>,</span><span style="color: black;"> <span style="color: black;">"detect"</span>:<span style="color: black;">"zh-CHS"</span></span><span style="color: black;">}</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(注:我将自己的PID和KEY都<span style="color: black;">隐匿</span>了,你<span style="color: black;">能够</span>注册搜狗翻译API后将自己的PID和KEY填入其中)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">咱们</span>只想要译文的话,<span style="color: black;">能够</span>将JSON结果转换为PHP数组,再打印出来:</p><span style="color: black;"><span style="color: black;"><span style="color: black;">&lt;?php</span></span></span><span style="color: black;"><span style="color: black;">//创建一个“浏览器”</span></span><span style="color: black;">$ch = curl_init();</span><span style="color: black;"><span style="color: black;">//设置要发送的数据</span></span><span style="color: black;">$q = <span style="color: black;">"北京语言大学"</span>;</span><span style="color: black;">$from = <span style="color: black;">"zh-CHS"</span>;</span><span style="color: black;">$to = <span style="color: black;">"en"</span>;</span><span style="color: black;">$pid = "";</span><span style="color: black;">$key = "";</span><span style="color: black;">$salt = rand(<span style="color: black;">1</span>,<span style="color: black;">100</span>);</span><span style="color: black;">$sign = md5($pid.$q.$salt.$key);</span><span style="color: black;"><span style="color: black;">//将数据拼接成name=hanlintao&amp;password=bugaosuni形式</span></span><span style="color: black;">$data = <span style="color: black;">"q="</span>.$q.<span style="color: black;">"&amp;from="</span>.$from.<span style="color: black;">"&amp;to="</span>.$to.<span style="color: black;">"&amp;pid="</span>.$pid.<span style="color: black;">"&amp;salt="</span>.$salt.<span style="color: black;">"&amp;sign="</span>.$sign;</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”要<span style="color: black;">拜访</span>哪个链接</span></span><span style="color: black;">curl_setopt($ch, CURLOPT_URL,<span style="color: black;">"http://fanyi.sogou.com/reventondc/api/sogouTranslate"</span>);</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”发送和接收的数据格式</span></span><span style="color: black;">curl_setopt($ch, CURLOPT_HTTPHEADER, <span style="color: black;">array</span>(<span style="color: black;">"Content-Type:application/x-www-form-urlencoded"</span>,<span style="color: black;">"Accept:application/json"</span>));</span><span style="color: black;">curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span style="color: black;">1</span>);</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”用POST<span style="color: black;">办法</span>来发送拼接好的数据</span></span><span style="color: black;">curl_setopt($ch, CURLOPT_POSTFIELDS, $data);</span><span style="color: black;">curl_setopt($ch, CURLOPT_POST, <span style="color: black;">1</span>);</span><span style="color: black;"><span style="color: black;">//告诉“浏览器”把<span style="color: black;">拜访</span><span style="color: black;">目的</span>网址后得到的结果放在$output变量中</span></span><span style="color: black;">$output = curl_exec($ch);</span><span style="color: black;"><span style="color: black;">//打印结果</span></span><span style="color: black;">$ret = json_decode($output, <span style="color: black;">true</span>);</span><span style="color: black;"><span style="color: black;">echo</span> $ret[<span style="color: black;">"translation"</span>];</span><span style="color: black;"><span style="color: black;">//关闭“浏览器”</span></span><span style="color: black;">curl_close($ch);</span><span style="color: black;">?&gt;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运行后的结果为:</p><span style="color: black;">Beijing Language <span style="color: black;">and</span> Culture University</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span>许你看到<span style="color: black;">这儿</span>就懵了,怎么<span style="color: black;">忽然</span>冒出来这么多代码,不解释解释?</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实这段代码的<span style="color: black;">规律</span>很简单,<span style="color: black;">咱们</span>给<span style="color: black;">大众</span>说明一下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第1</span>步:<span style="color: black;">起步</span>“浏览器”</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">我<span style="color: black;">始终</span>在用“浏览器”来打比方,<span style="color: black;">咱们</span>既然想去<span style="color: black;">拜访</span>搜狗的<span style="color: black;">设备</span>翻译API,<span style="color: black;">那样</span>就得用cURL工具<span style="color: black;">起步</span>一个浏览器窗口,这段代码<span style="color: black;">便是</span>:</p>
    <span style="color: black;"><span style="color: black;">$ch</span> = curl_init();</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第二步:准备要发送的数据</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>要<span style="color: black;">按照</span>搜狗翻译API文档中提出的<span style="color: black;">必须</span>参数<span style="color: black;">需要</span>来准备要发送的数据,如下:</p><span style="color: black;">$q = <span style="color: black;">"北京语言大学"</span>;</span><span style="color: black;">$from = <span style="color: black;">"zh-CHS"</span>;</span><span style="color: black;">$to = <span style="color: black;">"en"</span>;</span><span style="color: black;">$pid = <span style="color: black;">""</span>;</span><span style="color: black;">$key = <span style="color: black;">""</span>;</span><span style="color: black;">$salt = <span style="color: black;">rand</span>(<span style="color: black;">1</span>,<span style="color: black;">100</span>);</span><span style="color: black;">$sign = md5($pid.$q.$salt.$key);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后一行的md5()函数<span style="color: black;">便是</span>用来加密的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">这般</span>的数据<span style="color: black;">不可</span>直接发送给搜狗,搜狗翻译说了,要拼接起来发给它,<span style="color: black;">因此</span>用了下面这段代码:</p><span style="color: black;"><span style="color: black;">$data</span> = <span style="color: black;">"q="</span>.<span style="color: black;">$q</span>.<span style="color: black;">"&amp;from="</span>.<span style="color: black;">$from</span>.<span style="color: black;">"&amp;to="</span>.<span style="color: black;">$to</span>.<span style="color: black;">"&amp;pid="</span>.<span style="color: black;">$pid</span>.<span style="color: black;">"&amp;salt="</span>.<span style="color: black;">$salt</span>.<span style="color: black;">"&amp;sign="</span>.<span style="color: black;">$sign</span>;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">效果如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">q=北京语言大学&amp;from=zh-CHS&amp;to=en&amp;pid=***&amp;salt=63&amp;sign=***</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">搜狗翻译只接受<span style="color: black;">这般</span>的数据格式。</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第三步:告诉浏览器要发送什么网址</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">代码如下:</span></p><span style="color: black;">curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_URL, <span style="color: black;">"http://fanyi.sogou.com/reventondc/api/sogouTranslate"</span>);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第四步:告诉浏览器<span style="color: black;">咱们</span>发送给它的数据格式是<span style="color: black;">怎么样</span>的</p><span style="color: black;">curl_setopt($ch, CURLOPT_HTTPHEADER,</span><span style="color: black;"><span style="color: black;">array</span>(</span><span style="color: black;"><span style="color: black;">"Content-Type:application/x-www-form-urlencoded"</span>,</span><span style="color: black;"><span style="color: black;">"Accept:application/json"</span>)</span><span style="color: black;">);</span><span style="color: black;">curl_setopt($ch, CURLOPT_RETURNTRANSFER,<span style="color: black;">1</span>);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大众</span><span style="color: black;">此刻</span>看到<span style="color: black;">咱们</span>最<span style="color: black;">起始</span>的那个截图中的设置是怎么反映到代码中的吧?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第五步:把数据<span style="color: black;">经过</span>浏览器用POST<span style="color: black;">办法</span>发送出去</p><span style="color: black;">curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: black;">$data</span>);</span><span style="color: black;">curl_setopt(<span style="color: black;">$ch</span>, CURLOPT_POST, 1);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面这两段代码<span style="color: black;">便是</span>告诉浏览器请用POST<span style="color: black;">办法</span>来发送$data中拼接好的数据</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第六步:把浏览器<span style="color: black;">得到</span>的结果发送一个变量中,准备打印</p><span style="color: black;"><span style="color: black;">$output</span> = curl_exec(<span style="color: black;">$ch</span>);</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实到了这一步才是真正按回车键去发送调用<span style="color: black;">设备</span>翻译API的命令,前面的都是进行设置,<span style="color: black;">因此</span><span style="color: black;">她们</span>的前缀才是curl_setopt(),opt<span style="color: black;">便是</span>option的意思。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第七步:将获取到的数据转换成数组再展示出来</p><span style="color: black;"><span style="color: black;">$ret</span> = json_decode(<span style="color: black;">$output</span>, <span style="color: black;">true</span>);</span><span style="color: black;"><span style="color: black;">echo</span> <span style="color: black;">$ret</span>[<span style="color: black;">"translation"</span>];</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>搜狗翻译返回给<span style="color: black;">咱们</span>的数据是JSON格式的,PHP<span style="color: black;">没法</span>直接打印出来其中的某些元素,<span style="color: black;">因此呢</span><span style="color: black;">咱们</span>需要用json_decode()函数将JSON格式的数据转换为数组,<span style="color: black;">而后</span>再将<span style="color: black;">设备</span>翻译的译文展示出来。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上<span style="color: black;">便是</span>用PHP调用搜狗<span style="color: black;">设备</span>翻译API的全过程,这应该是<span style="color: black;">大众</span>能找到的最简单的PHP代码示例了~</p>





j8typz 发表于 2024-10-12 17:00:59

我深感你的理解与共鸣,愿对话长流。

m5k1umn 发表于 2024-10-13 05:55:15

顶楼主,说得太好了!

wrjc1hod 发表于 2024-10-28 17:37:17

你的言辞如同繁星闪烁,点亮了我心中的夜空。

m5k1umn 发表于 2024-11-12 21:04:07

这夸赞甜到心里,让我感觉温暖无比。

qzmjef 发表于 4 天前

楼主果然英明!不得不赞美你一下!
页: [1]
查看完整版本: PHP是怎么样调用设备翻译API的—以搜狗翻译为例