为了账号安全,请及时绑定邮箱和手机立即绑定

如何将免费的 google translate api 与 codeigniter

如何将免费的 google translate api 与 codeigniter

PHP
呼唤远方 2021-06-16 16:05:29
我正在尝试将 Google translate Api 与 codeigniter 集成,它首先可以工作,但在几次请求后我收到此错误302 Moved,似乎服务器阻止了我的 IP 地址。<?phpclass GoogleTranslate{    public static function translate($source, $target, $text)    {        $response = self::requestTranslation($source, $target, $text);                 $translation = self::getSentencesFromJSON($response);        return $translation;    }    protected static function requestTranslation($source, $target, $text)    {               $url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e";        $fields = array(            'sl' => urlencode($source),            'tl' => urlencode($target),            'q' => urlencode($text)        );        $fields_string = "";        foreach ($fields as $key => $value) {            $fields_string .= $key . '=' . $value . '&';        }        rtrim($fields_string, '&');               $ch = curl_init();               curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, count($fields));        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);        curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');       $result = curl_exec($ch);        curl_close($ch);        return $result;    }        protected static function getSentencesFromJSON($json)    {        $sentencesArray = json_decode($json, true);        $sentences = "";        foreach ($sentencesArray["sentences"] as $s) {            $sentences .= isset($s["trans"]) ? $s["trans"] : '';        }        return $sentences;    }}我使用一个包含许多应该翻译的文本的数组。如何在不被屏蔽的情况下使用 Google 翻译 Api?
查看完整描述

2 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

使用官方谷歌翻译 API,见https://cloud.google.com/translate/docs/

Google 每个月都会免费为您提供 10 美元的信用额度。这意味着每月免费翻译 500 000 个字符。


查看完整回答
反对 回复 2021-06-25
?
智慧大石

TA贡献1946条经验 获得超3个赞

只需要创建一个函数 translatetext()


function translatetext($text){

    $curlSession = curl_init(); 

    curl_setopt($curlSession, CURLOPT_URL, 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&q='.urlencode($text));

    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);

    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($curlSession);

    $jsonData = json_decode($response);

    curl_close($curlSession);


    if(isset($jsonData[0][0][0])){

        return $jsonData[0][0][0];

    }else{

        return false;

    }

}

$text = '卡姆拉尼赫鲁纳加尔';


回声翻译文本($文本);


输出:कमलानेहरूनगर


查看完整回答
反对 回复 2021-06-25
  • 2 回答
  • 0 关注
  • 279 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号