帮助文档  > 产品文档 > API资源 > 聊天机器人

翻译服务将在2019年4月19日零点停止服务

如需体验最新的搜狗翻译技术,请访问https://deepi.sogou.com

搜狗AI技术服务 之 中英互译

服务测试地址

http://api.ai.sogou.com/pub/nlp/translate

参数说明

参数名类型说明
qstringutf8 编码的 urlencode 格式。不超过 5000 个字符
fromstring取值范围为 ["en", "zh-CHS"]
tostring取值范围为 ["zh-CHS", "en"]

请求示例

<?php

function sign($ak, $sk, $url, $method) {
    /*
     * 生成app签名函数
     */
    $res = parse_url($url);
    $hst = $res["host"];
    $uri = $res["path"];

    if ($res["query"]) {
        parse_str($res["query"], $arg); ksort($arg);
    } else {
        $arg = array();
    }

    $arg = http_build_query($arg);


    $pre = "sac-auth-v1/$ak/".time()."/3600";
    $calc = "$pre\n$method\n$hst\n$uri\n$arg";

    return "$pre/".base64_encode(hash_hmac('sha256', $calc, $sk, true));
}

$now = intval(gettimeofday(true) * 1000);

// translate测试应用:ak&sk
$ak = "";
$sk = "";

// q: 待翻译字符串
$q = urlencode("买椟还珠");
// from: 源语言
$from = "zh-CHS";
// to: 目标语言
$to = "en";

// 请求URL
$url = "http://api.ai.sogou.com/pub/nlp/translate?q=$q&from=$from&to=$to";

$hdr = array(
    "Content-Type: application/json",
    "Authorization: ".sign($ak, $sk, $url, "GET")
);

$now = gettimeofday(true);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $hdr);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$res = curl_exec($curl);
curl_close($curl);

$json = json_decode($res, true);
print_r($json);

echo "\ncost: " . sprintf("%.2f", gettimeofday(true) - $now)."\n";
?>

返回结果

Array
(
    [status] => 0
    [trans_result] => Array
        (
            [0] => Array
                (
                    [success] => 1
                    [trans_text] => show lack of judgement as one who buys the glittering casket and return the pearls to the seller
                )

        )

)

cost: 0.06

版本记录

  • v 0.0 初次上线,中英互译服务