谷歌翻譯使用php curl請求接口文檔


支持語言


REST API

Method
translate POST /language/translate/v2 (Translates input text, returning translated text.)
detect POST /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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM