You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PHP发送POST请求调用DeepL翻译API?

适配DeepL翻译接口的PHP实现代码

直接替换密钥即可运行的完整代码如下:

<?php
// 付费版接口地址,免费版请替换为 https://api-free.deepl.com/v2/translate
$url = 'https://api.deepl.com/v2/translate';
// POST请求参数,完全对应官方文档要求的字段
$data = [
    'auth_key' => '替换为你自己的DeepL接口密钥',
    'text' => 'this is a test',
    'target_lang' => 'de'
];

$options = [
    'http' => [
        // 补全官方要求的所有请求头,多个头用\r\n分隔
        'header' => "Content-type: application/x-www-form-urlencoded\r\n".
                    "Accept: */*\r\n".
                    "User-Agent: YourApp\r\n",
        'method' => 'POST',
        'content' => http_build_query($data)
    ]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
    // 可自行添加请求失败的处理逻辑
    die('接口请求失败');
}

// 接口返回JSON格式数据,按需解析即可
var_dump(json_decode($result, true));
?>
参数对应说明
  • 接口地址:对应官方请求示例的Host+请求路径,拼接为https://api.deepl.com/v2/translate,必须使用HTTPS协议,免费版用户需更换为免费版专属域名
  • POST参数:$data数组的三个键完全对应官方请求体的三个字段:
    • auth_key:替换为你申请的DeepL接口密钥
    • text:填写需要翻译的文本内容
    • target_lang:填写目标语言代码,比如德语为de、简体中文为zh
  • 请求头:除了必填的Content-type,额外补充官方示例要求的Accept和User-Agent字段,避免接口校验失败

内容的提问来源于stack exchange,提问作者Phpnewb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 22:24:03