美文网首页
thinkphp实现中英文之间百度翻译 --- 2020-09-

thinkphp实现中英文之间百度翻译 --- 2020-09-

作者: 一位先生_ | 来源:发表于2020-09-07 10:03 被阅读0次

1.首先要去百度翻译开放平台认证并开通翻译服务
https://api.fanyi.baidu.com/api/trans/product/desktop?req=developer

image.png

注意:翻译的时候有ip限制,要把自己电脑所属的ip地址加上去

<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
class Base extends Controller
{
 
    /** 百度翻译入口
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2020/8/27
     * Time: 9:02
     */
    function translate($query, $from='auto', $to='zh')
    {
        $fy_url    =  'http://api.fanyi.baidu.com/api/trans/vip/translate';
        $app_id =   '你百度翻译认证开通后的appid';
        $sec_key =   '你百度翻译认证开通后的秘钥';
        $args = array(
            'q' => $query,
            'appid' => $app_id,
            'salt' => rand(10000,99999),
            'from' => $from,
            'to' => $to,
        );
        $args['sign'] = $this->buildSign($query, $app_id, $args['salt'], $sec_key);
        $ret = $this->call($fy_url, $args);
        $ret = json_decode($ret, true);
        return $ret;
    }

    //加密
    function buildSign($query, $appID, $salt, $secKey)
    {
        $str = $appID . $query . $salt . $secKey;
        $ret = md5($str);
        return $ret;
    }

    //发起网络请求
    function call($url, $args=null, $method="post", $testflag = 0, $timeout = 10, $headers=array())
    {
        $ret = false;
        $i = 0;
        while($ret === false)
        {
            if($i > 1)
                break;
            if($i > 0)
            {
                sleep(1);
            }
            $ret = $this->callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }

    function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = 10, $headers=array())
    {
        $ch = curl_init();
        if($method == "post")
        {
            $data = $this->convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        else
        {
            $data = convert($args);
            if($data)
            {
                if(stripos($url, "?") > 0)
                {
                    $url .= "&$data";
                }
                else
                {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(!empty($headers))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if($withCookie)
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }

    function convert(&$args)
    {
        $data = '';
        if (is_array($args))
        {
            foreach ($args as $key=>$val)
            {
                if (is_array($val))
                {
                    foreach ($val as $k=>$v)
                    {
                        $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                    }
                }
                else
                {
                    $data .="$key=".rawurlencode($val)."&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }


}

?>

调用翻译接口

$this->translate($query='要翻译的文字', $from='auto', $to='en');

完美解决!!!

相关文章

网友评论

      本文标题:thinkphp实现中英文之间百度翻译 --- 2020-09-

      本文链接:https://www.haomeiwen.com/subject/xjjkektx.html