очень нужна ваша помощь
Форма http://soundmaster.web-student.ru/test4.php
Перевод с помощью translate.yandex.ru
1. Добавить возможность загружать текст для перевода из файла с расширением txt не более 1000 символов
2. Добавить возможность сохранять перевод в файл с расширением txt
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post">
<div>
<select name="direction">
<option value="en-ru" <?php if (isset($_REQUEST['direction']) && $_REQUEST['direction'] == 'en-ru') { echo 'selected'; }?> >С английского на русский</option>
<option value="ru-en" <?php if (isset($_REQUEST['direction']) && $_REQUEST['direction'] == 'ru-en') { echo 'selected'; }?> >С русского на английский</option>
</select>
</div>
<div>
<textarea name="text" id="" cols="30" rows="10"><?php if (isset($_REQUEST['text'])) { echo $_REQUEST['text']; }?></textarea>
</div>
<div>
<input type="submit" value="Перевести">
</div>
</form>
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$text = $_REQUEST['text'];
$direction = $_REQUEST['direction'];
$translate = translate($text, $direction);
} ?>
<div>
<textarea id="" cols="30" rows="10"><?php if (isset($translate)) { echo $translate; }?></textarea>
</div>
<?
function translate($_str, $_direction) {
$curlHandle = curl_init();
// options
$postData=array();
$postData['text']= $_str;
$key = 'trnsl.1.1.20170109T113422Z.790f54c88e4bd95e.5f913e2a67ced8f0ffab11c920d21725d2bc9d40';
curl_setopt($curlHandle, CURLOPT_URL, "https://translate.yandex.net/api/v1.5/tr.json/translate?lang=$_direction&key=$key");
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array(
'User-Agent: Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: ru,en-us;q=0.7,en;q=0.3',
'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7',
'Keep-Alive: 300',
'Connection: keep-alive'
));
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
curl_setopt($curlHandle, CURLOPT_POST, 0);
if ( $postData!==false ) {
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, http_build_query($postData));
}
$content = curl_exec($curlHandle);
curl_close($curlHandle);
$content = str_replace(',,',',"",',$content);
$content = str_replace(',,',',"",',$content);
$result = json_decode($content);
return $result->text[0];
}?>
</body>