/** * See http://blogs.msdn.com/b/translation/p/phptranslator.aspx */ public function translate_string($string, $source_language_code, $target_language_code) { //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $this->clientId, $this->clientSecret, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Set the params.// $params = "text=" . urlencode($string) . "&to=" . $target_language_code . "&from=" . $source_language_code; $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}"; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Get the curlResponse. $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader); //Interprets a string of XML into an object. $xmlObj = simplexml_load_string($curlResponse); if (strpos($xmlObj->body->h1, "Exception") !== false) { throw new AutomaticTranslationException("Fehler bei der automatischen Uebersetzung der Inhalte nach {$target_language_code}: " . implode(", ", (array) $xmlObj->body->p)); } $translatedStr = ""; foreach ((array) $xmlObj[0] as $val) { $translatedStr .= $val; } return $translatedStr; }
protected function getAuthHeader($clientID, $clientSecret) { if (!is_null($this->authheader)) { return $this->authheader; } try { require __DIR__ . '/azuretoken.php'; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create the authorization Header string. $this->authHeader = "Authorization: Bearer " . $accessToken; return $this->authHeader; } catch (Exception $e) { JError::raiseWarning('SOME_ERROR_CODE', $e->getMessage()); return false; } }
function traducir($lineas, $origen, $destino) { $traduccion = array(); //Client ID of the application. $clientID = "TU ID DE CLIENTE"; //Client Secret key of the application. $clientSecret = "TU ID DE APLICACION o SECRETO DE CLIENTE"; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Set the Params. $fromLanguage = $origen; $toLanguage = $destino; $user = '******'; $category = "general"; $uri = null; $contentType = "text/html"; $maxTranslation = 5; //Input text Array. $inputStrArr = array(utf8_encode(html_entity_decode(special_char($lineas[0]))), utf8_encode(html_entity_decode(special_char($lineas[1]))), utf8_encode(html_entity_decode(special_char($lineas[2]))), utf8_encode(html_entity_decode(special_char($lineas[3]))), utf8_encode(html_entity_decode(special_char($lineas[4])))); /*print_r($inputStrArr); die;*/ //HTTP GetTranslationsArray Method Url. $getTranslationUrl = "http://api.microsofttranslator.com/V2/Http.svc/GetTranslationsArray"; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Get the Request XML Format. $requestXml = $translatorObj->createReqXML($fromLanguage, $toLanguage, $category, $contentType, $user, $inputStrArr, $maxTranslation); //Call HTTP Curl Request. $curlResponse = $translatorObj->curlRequest($getTranslationUrl, $authHeader, $requestXml); // Interprets a string of XML into an object. $xmlObj = simplexml_load_string($curlResponse); $translationResponse = $xmlObj->GetTranslationsResponse; $y = 0; foreach ($translationResponse as $translationArr) { $translationMatchArr = $translationArr->Translations->TranslationMatch; echo $inputStrArr[$y] . " = "; foreach ($translationMatchArr as $translationMatch) { echo $translationMatch->TranslatedText . '<br>'; $traduccion[] = $translationMatch->TranslatedText; } $y++; } return $traduccion; }
public function getServiceToken($reset = false) { static $accessToken = null; if (!$accessToken || $reset) { $clientID = $this->params->get('bing_client_id', ''); $clientSecret = $this->params->get('bing_client_secret', ''); $authUrl = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/'; $scopeUrl = 'http://api.microsofttranslator.com'; $grantType = 'client_credentials'; try { //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); } catch (Exception $e) { jexit('<span class="failed">' . $e->getMessage() . '</span>'); } } return $accessToken; }
function refreshAccessToken() { // Client ID of the application. $clientID = "YOUR_ID"; // Client Secret key of the application. $clientSecret = "YOUR_SECRET"; // OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; // Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; // Application grant type $grantType = "client_credentials"; // Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); // Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); // Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; return $authHeader; }
function translate($text, $from = 'en', $to = 'fr') { /* //Client ID of the application. $clientID = "8549f6eb-44ad-4fa3-a2b5-7fc0935fcebe"; //Client Secret key of the application. $clientSecret = "lnzsGfzAExyJmLrYIGfKkEiDGZR/PUik/T1SjPS+BhM="; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; */ //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. die($this->clientSecret); $accessToken = $authObj->getTokens($this->grantType, $this->scopeUrl, $this->clientID, $this->clientSecret, $this->authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Set the params.// $fromLanguage = $from; $toLanguage = $to; $inputStr = $text; $contentType = 'text/html'; $category = 'general'; $params = "text=" . urlencode($inputStr) . "&to=" . $toLanguage . "&from=" . $fromLanguage; $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}"; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Get the curlResponse. $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader); print_r($curlResponse); //Interprets a string of XML into an object. $xmlObj = simplexml_load_string($curlResponse); foreach ((array) $xmlObj[0] as $val) { $translatedStr = $val; } return $translatedStr; }
public function translatorApi($inputStr = '', $fromLanguage = 'en', $toLanguage = 'zh-cn') { try { //Client ID of the application. $clientID = "template"; //Client Secret key of the application. $clientSecret = "uex3LMUNRYO2nwMwkhrOec6D4kTKbuYMjKoRuAW0K0M="; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Set the params.// $contentType = 'text/plain'; $category = 'general'; $params = "text=" . urlencode($inputStr) . "&to=" . $toLanguage . "&from=" . $fromLanguage; $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}"; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Get the curlResponse. $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader); //Interprets a string of XML into an object. $xmlObj = simplexml_load_string($curlResponse); foreach ((array) $xmlObj[0] as $val) { $translatedStr = $val; } return $translatedStr; } catch (Exception $e) { echo "Exception: " . $e->getMessage() . PHP_EOL; } }
function csp_po_ajax_handle_translate_by_microsoft() { csp_po_check_security(); if (!defined('TRANSLATION_API_PER_USER_DONE')) { csp_po_init_per_user_trans(); } $msgid = $_POST['msgid']; $search = array('\\\\\\\\"', '\\\\\\"', '\\\\n', '\\\\r', '\\\\t', '\\\\$', '\\0', "\\'", '\\\\'); $replace = array('\\"', '"', "\n", "\r", "\\t", "\\\$", "", "'", "\\"); $msgid = str_replace($search, $replace, $msgid); require_once 'includes/translation-api-microsoft.php'; header('Content-Type: text/plain'); try { //Client ID of the application. $clientID = defined('MICROSOFT_TRANSLATE_CLIENT_ID') ? MICROSOFT_TRANSLATE_CLIENT_ID : ''; //Client Secret key of the application. $clientSecret = defined('MICROSOFT_TRANSLATE_CLIENT_SECRET') ? MICROSOFT_TRANSLATE_CLIENT_SECRET : ''; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Set the params.// $fromLanguage = "en"; $toLanguage = strip_tags($_POST['destlang']); $inputStr = $msgid; $contentType = 'text/plain'; $category = 'general'; $params = "text=" . urlencode($inputStr) . "&to=" . $toLanguage . "&from=" . $fromLanguage; $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}"; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Get the curlResponse. $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader); //Interprets a string of XML into an object. $xmlObj = simplexml_load_string($curlResponse); foreach ((array) $xmlObj[0] as $val) { $translatedStr = $val; } echo $translatedStr; } catch (Exception $e) { header('Status: 404 Not Found'); header('HTTP/1.1 404 Not Found'); echo $e->getMessage(); } exit; }
return $curlResponse; } } try { //Client ID of the application. $clientID = "8549f6eb-44ad-4fa3-a2b5-7fc0935fcebe"; //Client Secret key of the application. $clientSecret = "lnzsGfzAExyJmLrYIGfKkEiDGZR/PUik/T1SjPS+BhM="; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Set the params.// $fromLanguage = "en"; $toLanguage = "fr"; $inputStr = "the best machine translation technology cannot always provide translations tailored to a site or users like a human"; $contentType = 'text/html'; $category = 'general'; $params = "text=" . urlencode($inputStr) . "&to=" . $toLanguage . "&from=" . $fromLanguage; $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}"; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Get the curlResponse.
function testTranslate() { try { //Client ID of the application. $clientID = "0f442800-138b-4f8d-b1c6-706cea5924d5"; //Client Secret key of the application. $clientSecret = "H+Lj+Wwbp+T+vRtve/+lcKSO+6a70nSPPwElnI7jgKE="; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Create the Translator Object. $translatorObj = new HTTPTranslator(); //Input String. $inputStr = 'This is the sample string.'; //HTTP Detect Method URL. $detectMethodUrl = "http://api.microsofttranslator.com/V2/Http.svc/Detect?text=" . urlencode($inputStr); //Call the curlRequest. $strResponse = $translatorObj->curlRequest($detectMethodUrl, $authHeader); //Interprets a string of XML into an object. $xmlObj = simplexml_load_string($strResponse); foreach ((array) $xmlObj[0] as $val) { $languageCode = $val; } /* * Get the language Names from languageCodes. */ $locale = 'en'; $getLanguageNamesurl = "http://api.microsofttranslator.com/V2/Http.svc/GetLanguageNames?locale={$locale}"; //Create the Request XML format. $requestXml = $translatorObj->createReqXML($languageCode); //Call the curlRequest. $curlResponse = $translatorObj->curlRequest($getLanguageNamesurl, $authHeader, $requestXml); //Interprets a string of XML into an object. $xmlObj = simplexml_load_string($curlResponse); echo "<table border=2px>"; echo "<tr>"; echo "<td><b>LanguageCodes</b></td><td><b>Language Names</b></td>"; echo "</tr>"; foreach ($xmlObj->string as $language) { echo "<tr><td>" . $inputStr . "</td><td>" . $languageCode . "(" . $language . ")" . "</td></tr>"; } echo "</table>"; } catch (Exception $e) { echo "Exception: " . $e->getMessage() . PHP_EOL; } }
function get_youtube_update($provider, $url) { if ($provider == "LEGO") { // Prepare the MSFT Langurage Detector try { //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens($grantType, $scopeUrl, MS_TRANSLATOR_CLIENTID, MS_TRANSLATOR_CLIENTSECRET, $authUrl); //Create the authorization Header string. $authHeader = "Authorization: Bearer " . $accessToken; //Create the Translator Object. $translatorObj = new HTTPTranslator(); } catch (Exception $e) { echo "Exception: " . $e->getMessage() . PHP_EOL; } } $ret = new stdClass(); $ret->{'PubDate'} = time(); $ret->{'Provider'} = $provider; $ret->{'Url'} = $url; $ret->{'Type'} = "Video-Youtube"; $arrNews = array(); $xmlDom = new DOMDocument(); $xmlDom->load($url); $xml = simplexml_import_dom($xmlDom); foreach ($xml->entry as $entry) { $media = $entry->children('media', true); $item = new stdClass(); $item->{'Provider'} = $ret->{'Provider'}; $item->{'Type'} = $ret->{'Type'}; $item->{'Hash'} = (string) $entry->children('yt', true)->videoId; $item->{'Title'} = (string) $media->group->title; $item->{'Link'} = "https://youtu.be/" . $item->{'Hash'}; $date = new DateTime($entry->published, new DateTimeZone("UTC")); $item->{'PubDate'} = $date->format('U'); if ($item->{'PubDate'} < $ret->{'PubDate'}) { $ret->{'PubDate'} = $item->{'PubDate'}; } //$item->{'updated'} = strtotime($entry->updated); //$item->{'url'} = (string)$media->group->content->attributes()['url']; //$item->{'thumbnail'} = (string)$media->group->thumbnail->attributes()['url']; $item->{'PicPath'} = null; $item->{'Publish'} = true; $item->{'Review'} = false; if ($provider == "LEGO") { $desc = (string) $media->group->description; $detectMethodUrl = "http://api.microsofttranslator.com/V2/Http.svc/Detect?text=" . urlencode($item->{'Title'} . " " . $desc); $strResponse = $translatorObj->curlRequest($detectMethodUrl, $authHeader); $xmlObj = simplexml_load_string($strResponse); foreach ((array) $xmlObj[0] as $val) { $language = $val; } if ($language != "en") { $item->{'Publish'} = false; } $arrNews[$item->{'Hash'}] = $item; } else { $arrNews[$item->{'Hash'}] = $item; } } $ret->{'News'} = $arrNews; return $ret; }
public function refreshAccessToken() { //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $this->accessToken = $authObj->getTokens($this->grantType, $this->scopeUrl, $this->clientID, $this->clientSecret, $this->authUrl); //Create the authorization Header string. return; }
function getLanguages() { try { //Soap WSDL Url $wsdlUrl = "http://api.microsofttranslator.com/V2/Soap.svc"; //Client ID of the application. $clientID = "yiiTranslator"; //Client Secret key of the application. $clientSecret = "sZWwENrl6WNWEHK6kHLPQ+Escwn3IGnobdmvW2LWWG0="; //OAuth Url. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; //Application Scope Url $scopeUrl = "http://api.microsofttranslator.com"; //Application grant type $grantType = "client_credentials"; //Create the Authentication object $authObj = new AccessTokenAuthentication(); //Get the Access token $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); //Create soap translator Object $soapTranslator = new SOAPMicrosoftTranslator($accessToken, $wsdlUrl); // passing argument list.// $requestArg = array('appId' => ''); //Call to getLanguagesForTranslate Method. $responseObj = $soapTranslator->objSoap->GetLanguagesForTranslate($requestArg); $languageCodes = $responseObj->GetLanguagesForTranslateResult; $languageCodeArr = array(); foreach ($languageCodes->string as $language) { $languageCodeArr[] = $language; } /* * Retrieves friendly names ffor the languages passed in languageCodes. */ $locale = "en"; //Request argument list. $requestArg = array('locale' => $locale, 'languageCodes' => $languageCodeArr); $responseObj = $soapTranslator->objSoap->GetLanguageNames($requestArg); $languageNames = $responseObj->GetLanguageNamesResult; $i = 0; echo "<table border=2px>"; echo "<tr>"; echo "<td><b>LanguageCodes</b></td><td><b>Language Names</b></td>"; echo "</tr>"; foreach ($languageNames->string as $language) { echo "<tr><td>" . $languageCodeArr[$i] . "</td><td>" . $language . "</td></tr>"; $i++; } echo "</table>"; } catch (Exception $e) { echo "Exception: " . $e->getMessage() . "<br/>"; } }
function bing_text_translate($text, $lang_from = 'en', $lang_to = 'ru') { global $settings, $seftranslate_error; //DBQuery $database =& JFactory::getDBO(); $ret_value = $text; $lang_from2 = $lang_from; $lang_to2 = $lang_to; if ($lang_from == 'pt-PT') { $lang_from2 = 'pt'; } if ($lang_to == 'pt-PT') { $lang_to2 = 'pt'; } if ($lang_from == 'zh-CN') { $lang_from2 = 'zh-CHS'; } if ($lang_to == 'zh-CN') { $lang_to2 = 'zh-CHS'; } if ($lang_from == 'zh-TW') { $lang_from2 = 'zh-CHT'; } if ($lang_to == 'zh-TW') { $lang_to2 = 'zh-CHT'; } $params = array(); if (!empty($settings['api_bing_client_secret']) && !empty($settings['api_bing_client_id'])) { //Create the AccessTokenAuthentication object. $authObj = new AccessTokenAuthentication(); //Get the Access token. $accessToken = $authObj->getTokens(); $params['appId'] = "Bearer" . " " . $accessToken; } else { if ($settings['debug']) { throw "Please set bing client secret and client id"; exit; } } $params['text'] = $text; $params['from'] = $lang_from2; $params['to'] = $lang_to2; $params['contentType'] = "text/html"; try { $bing_client = new SoapClient("http://api.microsofttranslator.com/V2/SOAP.svc"); $result = $bing_client->Translate($params); $ret_value = html_entity_decode($result->TranslateResult, ENT_NOQUOTES, 'UTF-8'); } catch (Exception $e) { if ($settings['debug']) { throw $e; exit; } else { $seftranslate_error = true; return $ret_value; } } if (trim($ret_value) == "") { return $ret_value; } //save to database $efentity = new mosSefentity($database); $efentity->hash = md5($text); $efentity->entity_text = $ret_value; $efentity->lang_from = $lang_from; $efentity->lang_to = $lang_to; $efentity->hits = 1; $efentity->date = date("Y-m-d H:i:s"); $efentity->checkin(); $efentity->store(); return $ret_value; }