< コード例 >
<?php
/*
Google Translationリクエスト用スクリプト
ソース言語は英語に固定
*/
include '../DefaultHeaderIni.php';
include '../MySQLConnectConfig_aviation.php';
// ============= Const =============== //
$URL = "https://www.googleapis.com/language/translate/v2?key=";
$KEY = "??????????????????????????????????";
// =================================== //
// == リクエストパラメータ取得 == //
$q = $_POST["q"] ? $_POST["q"] : $_GET["q"];
$target = $_POST["target"] ? $_POST["target"] : $_GET["target"];
if (!$q) {
$q = "Good Morning";
}
if (!$target) {
$target = "in";
}
// 送信データ //
// リテラルは必ずはシングルクオテーションにする //
$postdata = array(
'q' => $q,
'source' => 'en',
'target' => $target
);
$postdata_json = json_encode($postdata);
$postdata_json = str_replace("\r", "", $postdata_json);
//exit;
$url = $URL.$KEY;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postdata_json,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json; charset=UTF-8',
"accept: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
else {
echo $response;
}
< リクエストForm data >
q: U A881 United Airlines B789 N17963 Landed from ORD to HND Last Direction 269 Runway 22 Squawk 3126 21Seconds ago target: ja
< レスポンス例 >
{ "data": { "translations": [ { "translatedText": "U A881 ユナイテッド航空 B789 N17963 ORD から HND に着陸 最後の方向 269 滑走路 22 スコーク 3126 21 秒前" } ] } }