2: 2015-12-14 (月) 12:16:39 iseki  |
現: 2015-12-16 (水) 00:06:16 iseki  |
| $api = '....'; | | $api = '....'; |
| $ggl = 'https://www.googleapis.com/language/translate/v2'; | | $ggl = 'https://www.googleapis.com/language/translate/v2'; |
| + | |
| $txt = ''; | | $txt = ''; |
| $src = ''; | | $src = ''; |
| $dst = ''; | | $dst = ''; |
| + | |
| if (isset($_GET['text'])) $txt = htmlentities($_GET['text'], ENT_QUOTES, 'utf-8'); | | if (isset($_GET['text'])) $txt = htmlentities($_GET['text'], ENT_QUOTES, 'utf-8'); |
| if (isset($_GET['fr'])) $src = $_GET['fr']; | | if (isset($_GET['fr'])) $src = $_GET['fr']; |
| if (isset($_GET['to'])) $dst = $_GET['to']; | | if (isset($_GET['to'])) $dst = $_GET['to']; |
| $osrc = $src; | | $osrc = $src; |
| + | |
| // | | // |
| if ($src==='auto' || empty($src)) { | | if ($src==='auto' || empty($src)) { |
- | $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); | + | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
- | $langs = array_reverse($langs); | + | $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
- | foreach($langs as $lang) { | + | $langs = array_reverse($langs); |
- | $ctr = substr($lang, 0, 2); | + | foreach($langs as $lang) { |
- | if ($ctr!=$dst) { | + | $ctr = substr($lang, 0, 2); |
- | $src = $ctr; | + | if ($ctr!=$dst) { |
- | break; | + | $src = $ctr; |
| + | break; |
| + | } |
| } | | } |
| } | | } |
| } | | } |
| } | | } |
| + | |
| // | | // |
| if (!empty($txt) && !empty($src) && !empty($dst) && strlen($src)==2 && strlen($dst)==2 && $src!=$dst) { | | if (!empty($txt) && !empty($src) && !empty($dst) && strlen($src)==2 && strlen($dst)==2 && $src!=$dst) { |
| // | | // |
| $url = $ggl.'?key='.$api.'&q='.rawurlencode($txt).'&source='.$src.'&target='.$dst; | | $url = $ggl.'?key='.$api.'&q='.rawurlencode($txt).'&source='.$src.'&target='.$dst; |
| + | |
| $hndl = curl_init($url); | | $hndl = curl_init($url); |
| curl_setopt($hndl, CURLOPT_RETURNTRANSFER, true); | | curl_setopt($hndl, CURLOPT_RETURNTRANSFER, true); |
| $resp = curl_exec($hndl); | | $resp = curl_exec($hndl); |
| curl_close($hndl); | | curl_close($hndl); |
| + | |
| $decd = json_decode($resp, true); | | $decd = json_decode($resp, true); |
| + | |
| if (array_key_exists('data', $decd)) { | | if (array_key_exists('data', $decd)) { |
| echo $decd['data']['translations'][0]['translatedText']; | | echo $decd['data']['translations'][0]['translatedText']; |
| } | | } |
| else { | | else { |
- | echo 'ERROR : Google Translator returns error!!'; | + | echo 'ERROR : Google Translate returns error!!'; |
| error_log('trans.php: osrc = '.$osrc.', src = '.$src.', dist = '.$dst.', text = '.$txt); | | error_log('trans.php: osrc = '.$osrc.', src = '.$src.', dist = '.$dst.', text = '.$txt); |
| error_log('trans.php: '.print_r($decd, true)); | | error_log('trans.php: '.print_r($decd, true)); |