HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/www.baofufacai.com/f.php
<?php
/**
 * 使用 Google 翻译 API 封装的 PHP 接口
 */

class GoogleTranslate {
    private $api_key; // 申请的 API Key

    public function __construct($api_key) {
        $this->api_key = $api_key;
    }

    // 翻译函数
    public function translate($text, $source_lang, $target_lang) {
        $url = 'https://translation.googleapis.com/language/translate/v2?key=' . $this->api_key;

        // 构造 API 请求参数
        $params = array(
            'q' => $text,
            'source' => $source_lang,
            'target' => $target_lang,
            'format' => 'text'
        );

        // 发送 API 请求并处理返回结果
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($curl);
        curl_close($curl);

        // 解析返回的结果
        $data = json_decode($result, true);
        if(isset($data['data']) && isset($data['data']['translations']) && count($data['data']['translations']) > 0) {
            return $data['data']['translations'][0]['translatedText'];
        }
        return null;
    }
}

// 使用示例:
$api_key = 'AIzaSyApV2SnkzCF9mrXDZqyM8mbvZpU-q0kS84'; // 替换为你自己的 API Key
$translate = new GoogleTranslate($api_key);
$text = "How to set the translation!"; // 待翻译的文本
$source_lang = 'en'; // 原始语言
$target_lang = 'zh-CN'; // 目标语言
$result = $translate->translate($text, $source_lang, $target_lang);
echo "翻译结果:{$result}";
?>