Ejemplo n.º 1
0
 public function getText()
 {
     $params = JComponentHelper::getParams('com_localise');
     $clientID = $params->get('clientID');
     $secret = $params->get('client_secret');
     if (empty($clientID) || empty($secret)) {
         $this->setError(JText::_('COM_LOCALISE_MISSING_CLIENTID_SECRET'));
         return '';
     }
     $app = JFactory::getApplication();
     $text = $app->input->getHtml('text');
     if (empty($text)) {
         $this->setError(JText::_('COM_LOCALISE_MISSING_TEXT'));
         return '';
     }
     $to = $app->input->getCmd('to');
     if (empty($to)) {
         $this->setError(JText::_('COM_LOCALISE_MISSING_TO_LANGUAGECODE'));
         return '';
     }
     $from = $app->input->getCmd('from');
     class_exists('HTTPTranslator') or (require dirname(__DIR__) . '/helpers/azuretranslator.php');
     $translator = new HTTPTranslator();
     return $translator->translate($clientID, $secret, $to, $text, $from);
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
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;
}
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
function translateWithBing($sourceLanguage, $targetLanguage, $textToTranslate)
{
    $textToTranslate = urlencode($textToTranslate);
    try {
        global $accessToken;
        $authHeader = "Authorization: Bearer " . $accessToken;
        $contentType = 'text/plain';
        $category = 'general';
        $params = "text=" . $textToTranslate . "&to=" . $targetLanguage . "&from=" . $sourceLanguage;
        $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}";
        $translatorObj = new HTTPTranslator();
        $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader);
        $xmlObj = simplexml_load_string($curlResponse);
        foreach ((array) $xmlObj[0] as $val) {
            $translatedStr = $val;
        }
        return $translatedStr;
    } catch (Exception $e) {
        echo "Exception: " . $e->getMessage() . PHP_EOL;
        return;
    }
}
 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;
     }
 }
Ejemplo n.º 7
0
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;
}
<?php

require_once '../application/MScredentials.php';
require_once '../application/HTTPTranslator.php';
try {
    //Set the params
    $toLanguage = "en";
    $inputStr = "Esto es una mesa";
    //Create the Translator Object.
    $translatorObj = new HTTPTranslator($clientID, $clientSecret, TRUE);
    // 1.- Translate String
    $translatedStr = $translatorObj->translate($inputStr, $toLanguage);
    echo "OK. Translation " . $inputStr . " ===> " . $translatedStr . "\n";
    // 2.- Detect Language
    $languageCode = $translatorObj->detectLanguage($inputStr);
    echo "OK. Language detection " . $inputStr . " ===> " . $languageCode . "\n";
    // 3.- Speak in Spanish
    $mp3 = $translatorObj->speak($translatedStr, 'en');
    echo "OK. Speak " . $translatedStr . " ===> " . "\n";
    $fp = fopen('../data/data.mp3', 'w');
    fwrite($fp, $mp3);
    fclose($fp);
    exec('"C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe" -Idummy C:\\Users\\Javier\\git\\WhatsAppTranslate\\WhatsAppTranslate\\data\\data.mp3');
} catch (Exception $e) {
    echo "Error. Exception: " . $e->getMessage() . PHP_EOL;
}
Ejemplo n.º 9
0
    //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.
    $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 "<table border=2px>";
    echo "<tr>";
    echo "<td><b>From {$fromLanguage}</b></td><td><b>To {$toLanguage}</b></td>";
    echo "</tr>";
    echo "<tr><td>" . $inputStr . "</td><td>" . $translatedStr . "</td></tr>";
    echo "</table>";
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage() . PHP_EOL;
Ejemplo n.º 10
0
 public function translateArray($sentences, $fields)
 {
     $accessToken = $this->getServiceToken();
     //Create the authorization Header string.
     $authHeader = "Authorization: Bearer " . $accessToken;
     try {
         //Create the Translator Object.
         $translatorObj = new HTTPTranslator();
         //Get the Request XML Format.
         $requestXml = $translatorObj->createReqXML($this->from, $this->to, $this->contentType, $sentences);
         //HTTP TranslateMenthod URL.
         $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
         //Call HTTP Curl Request.
         $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader, $requestXml);
         //Interprets a string of XML into an object.
         $xmlObj = simplexml_load_string($curlResponse);
         if (is_object($xmlObj)) {
             $i = 0;
             $translated = array();
             foreach ($xmlObj->TranslateArrayResponse as $translatedArrObj) {
                 if (isset($translatedArrObj->Error)) {
                     $this->setError((string) $translatedArrObj->Error);
                     return false;
                 }
                 $translated[$i++] = (string) $translatedArrObj->TranslatedText;
             }
             if (!count($translated)) {
                 $this->setError(nl2br(strip_tags($curlResponse)));
                 return false;
             }
             return $translated;
         } else {
             //var_dump($xmlObj);
             $error = preg_replace('/[\\s\\S]+<body[^>]*>([\\s\\S]+)<\\/body>[\\s\\S]*/i', '$1', (string) $curlResponse);
             $this->setError($error);
             return false;
         }
     } catch (Exception $e) {
         $this->setError($e->getMessage());
         return false;
     }
 }
Ejemplo n.º 11
0
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;
    }
}
 //Client Secret key of the application.
 $clientSecret = "OImstk8A7Tv+CK+xq2HMRizvO9ur4gBcPk9nprFJqNo=";
 //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 = $_POST["inputText"];
 //HTTP Detect Method URL.
 $detectMethodUrlKorean = "http://api.microsofttranslator.com/V2/Http.svc/Translate?Text=" . urlencode($inputStr) . "&To=ko";
 //Call the curlRequest.
 $strResponseKorean = $translatorObj->curlRequest($detectMethodUrlKorean, $authHeader);
 echo "Korean: " . $strResponseKorean . "<br>";
 $detectMethodUrlJapanese = "http://api.microsofttranslator.com/V2/Http.svc/Translate?Text=" . urlencode($inputStr) . "&To=ja";
 //Call the curlRequest.
 $strResponseJapanese = $translatorObj->curlRequest($detectMethodUrlJapanese, $authHeader);
 echo "Japanese: " . $strResponseJapanese . "<br>";
 $detectMethodUrlChinese = "http://api.microsofttranslator.com/V2/Http.svc/Translate?Text=" . urlencode($inputStr) . "&To=zh-CHS";
 //Call the curlRequest.
 $strResponseChinese = $translatorObj->curlRequest($detectMethodUrlChinese, $authHeader);
 echo "Chinese-Simplified: " . $strResponseChinese . "<br>";
Ejemplo n.º 13
0
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;
}
Ejemplo n.º 14
0
 //Client Secret key of the application.
 $clientSecret = "1ZE7GXvP0z43tkbHDarSf/tJ4qgnBSXVW98lM7JtZbI=";
 //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);
 //    print_r($strResponse);
 //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.
  */
Ejemplo n.º 15
0
    #customer-id: b4a9ce02-d33d-48dd-aa99-7e0b19aeb0c9 , client-id; reCh10
    //Client Secret key of the application.
    $clientSecret = "vWDJk2h2xhLTNJjJ7K5JNki2yfj5HRUMfprxUXtx1oY=";
    #clientSecret; 9k1EougsDuKEAwfOj1WItwLfNxn/QoyE2HXND17K0LA
    //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();
    //HTTP Detect Method URL.
    $detectMethodUrl = "http://api.microsofttranslator.com/V2/Http.svc/Detect?text=" . urlencode($_POST['string']);
    //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) {
        echo trim($val);
    }
} catch (Exception $e) {
    echo 'error';
    #echo "Exception: " . $e->getMessage() . PHP_EOL;
}
function translateText($text, $translateFrom = "en", $translateTo = "ar")
{
    global $authHeader;
    $text = trim($text);
    if (empty($text)) {
        return false;
    }
    echoN("*** translating ... [{$text}]");
    //return $text;
    try {
        if ($authHeader == null) {
            $authHeader = refreshAccessToken();
        }
        //echon($authHeader);
        /*
         * REFERENCE:
         * https://msdn.microsoft.com/en-us/library/ff512421.aspx
         */
        // Create the Translator Object.
        $translatorObj = new HTTPTranslator();
        //Set the params.//
        $fromLanguage = $translateFrom;
        $toLanguage = $translateTo;
        $contentType = 'text/plain';
        $category = 'general';
        $params = "text=" . urlencode($text) . "&to=" . $toLanguage . "&from=" . $fromLanguage;
        // HTTP Detect Method URL.
        $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?{$params}";
        // Call the curlRequest.
        $strResponse = $translatorObj->curlRequest($translateUrl, $authHeader);
        // Interprets a string of XML into an object.
        $xmlObj = simplexml_load_string($strResponse);
        //var_dump($xmlObj);
        if (empty($xmlObj[0])) {
            return null;
        }
        $translation = trim((string) $xmlObj[0]);
        //echoN($translation);
        //(string) casting to avoid SimpleXMLElement serialization problem
        // trim for endlines
        return $translation;
    } catch (Exception $e) {
        echo "Exception: " . $e->getMessage() . PHP_EOL;
    }
}