谷歌翻译使用php curl请求接口文档

支持语言


REST API

Method
translatePOST /language/translate/v2 (Translates input text, returning translated text.)
detectPOST /language/translate/v2/detect (Detects the language of text within a request.)

KEY

API_KEY =

实例

translate翻译

使用 curl 向 https://translation.googleapi... 端点发出请求。该 curl 命令中包含待翻译文本 (q)、源语言 (source) 以及目标语言 (target) 对应的 JSON。


$url = 'https://translation.googleapis.com/language/translate/v2?key=API_KEY';
$headers = array();
$headers[]='Content-Type: application/json';
    $data = [
        'q'=>'我最喜欢的城市是堪培拉',
        'source'=>'zh-CN',
        'target'=>'en',
        'format'=>'text',
        'model'=>''
    ];
    $data = json_encode($data);
$res = curl($url,'post',$data,$headers);
return $res;

特殊参数 model
The translation model. Can be either base to use the Phrase-Based Machine Translation (PBMT) model, or nmt to use the Neural Machine Translation (NMT) model. If omitted, then nmt is used.
If the model is nmt, and the requested language translation pair is not supported for the NMT model, then the request is translated using the base model.

返回值


"{
  "data": {
    "translations": [
      {
        "translatedText": "My favorite city is Canberra"
      }
    ]
  }
}
detect检测语言

检测请求中的语言,并返回语言代码


$url = 'https://translation.googleapis.com/language/translate/v2/detect?key=API_KEY';
$headers = array();
$headers[]='Content-Type: application/json';
    $data = [
        'q'=>'我最喜欢的城市是堪培拉'
    ];
    $data = json_encode($data);
$res = curl($url,'post',$data,$headers);
return $res;

返回值


"{
  "data": {
    "detections": [
      [
        {
          "confidence": 1,
          "isReliable": false,
          "language": "zh-CN"
        }
      ]
    ]
  }
}

原文地址:https://segmentfault.com/a/1190000016278417

转载于:https://www.cnblogs.com/lalalagq/p/9971584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值