<center> <p>Langer fliter:</p> <form method="post"> <button type="submit" width="50" name="lang_en">English</button> <button type="submit" width="50" name="lang_de">German</button> <button type="submit" width="50" name="lang_fr">French</button> <button type="submit" width="50" name="lang_ar">Arabic</button> </form> <?php require_once 'translate.class.php'; $BingTranslator = new BingTranslator('bernini2015', 'amj5+UpX6B7K2SO9cHT8dqeDKZfnWuPtxBjwgQQAyXg'); $raw_query = $_SESSION['query']; $proc_query = str_replace(" ", "%20", $raw_query); $proc_query = rawurlencode($proc_query); $translation_de = $BingTranslator->getTranslation('en', 'de', $raw_query); $translation_fr = $BingTranslator->getTranslation('en', 'fr', $raw_query); $translation_ar = $BingTranslator->getTranslation('en', 'ar', $raw_query); $proc_trans = str_replace(" ", "+", $translation_de); $proc_trans_de = rawurlencode($proc_trans); $proc_trans = str_replace(" ", "+", $translation_fr); $proc_trans_fr = rawurlencode($proc_trans); $proc_trans = str_replace(" ", "+", $translation_ar); $proc_trans_ar = rawurlencode($proc_trans); $ch = curl_init(); $htps = "http://52.32.49.169:8983/solr/proj3/select?defType=edismax&qf=text_en%20text_de%20text_fr%20text_ar%20&%20q=" . $proc_query . "%20OR%20" . $proc_trans_de . "%20OR%20" . $proc_trans_fr . "%20OR%20" . $proc_trans_ar . "&start=0&rows=50&wt=json&indent=true&facet=true&facet.field=lang&facet.field=tweet_hashtags"; //echo $htps;
} } $clientID = "jarvis59"; $clientSecret = "1ZE7GXvP0z43tkbHDarSf/tJ4qgnBSXVW98lM7JtZbI="; $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; $scopeUrl = "http://api.microsofttranslator.com"; $grantType = "client_credentials"; $authObj = new AccessTokenAuthentication(); $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); $authHeader = "Authorization: Bearer " . $accessToken; $translatorObj = new HTTPTranslator(); $inputStr = $_POST['query']; $detectMethodUrl = "http://api.microsofttranslator.com/V2/Http.svc/Detect?text=" . urlencode($inputStr); $strResponse = $translatorObj->curlRequest($detectMethodUrl, $authHeader); $xmlObj = simplexml_load_string($strResponse); foreach ((array) $xmlObj[0] as $val) { $langDetected = $val; } $BingTranslator = new BingTranslator('jarvis59', '1ZE7GXvP0z43tkbHDarSf/tJ4qgnBSXVW98lM7JtZbI='); if (!empty($_REQUEST['to'])) { $result = $BingTranslator->getTranslation($langDetected, $_REQUEST['to'], $_POST['query']); } else { $arrLang = ["en", "de", "fr", "ru", "ar"]; $result[] = str_replace("#", "%23", $_POST['query']); for ($i = 0; $i < 5; $i++) { if ($arrLang[$i] != $langDetected) { $result[] = $BingTranslator->getTranslation($langDetected, $arrLang[$i], $_POST['query']); } } } echo json_encode($result);
curl_setopt($ch, CURLOPT_URL, "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); $result = json_decode($result); if (!is_object($result) || empty($result->access_token)) { die('Problems'); } $this->_token = $result->access_token; curl_close($ch); } public function translate($content, $languageFrom = 'ru', $languageTo = 'de') { $postData = array('text' => $content, 'from' => $languageFrom, 'to' => $languageTo, 'contentType' => 'text/plain'); $ch = curl_init('http://api.microsofttranslator.com/V2/Http.svc/Translate?' . http_build_query($postData)); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $this->_token)); $translation = curl_exec($ch); return trim($translation); } } $text = 'Виталий - это работает!!!'; $bing = new BingTranslator(); $bing->getToken(); $translation = $bing->translate($text); file_put_contents('trans.txt', [$text => $translation]); $echo = file_get_contents('trans.txt', $translation); echo $echo;
/** * Translation */ public function translateAction(Request $request) { $bingtranslator = new \BingTranslator('noucoze-dev', 'F/zGjS5yMyBr858o54uv3Xn7jCLRw6Wc1Ps8iJ/cE1w='); if ($request->isXmlHttpRequest()) { $body = $request->request->get('body'); $fromLang = $bingtranslator->getDetectedLanguage($body); $toLang = $request->getLocale(); $transString = $bingtranslator->getTranslation($fromLang, 'en', $body); $json = json_encode(array('translation' => $transString)); return new Response($json); } //return new Response($bingtranslator->getToken()); }