Live Helper Chat support forum.. Forum is locked. New place for questions - Github Discussions

You are not logged in.

Announcement

#1 2018-04-17 22:30:44

pkedpker
Member
Registered: 2018-04-17
Posts: 2

Microsoft Translator has moved and now requires new API

Can you add translation service https://tech.yandex.com/translate/  yandex? while it's free since the microsoft service is no longer free they offer 30 days free but you need to put in credit card in order to activate it.



Microsoft Translator has moved and now requires new API.

Here's the new code from a blog site

class BingTranslator {
    
    private $azure_key = "<< ENTER KEY1 >>";  // !!! TODO: key1 here !!!
   
  
    // Get the AZURE token
    function getToken($azure_key)
    {
        $url = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken';
        $ch = curl_init();
        $data_string = json_encode('{body}');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string),
                'Ocp-Apim-Subscription-Key: ' . $azure_key
            )
        );
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $strResponse = curl_exec($ch);
        curl_close($ch);
        return $strResponse;
    }
    
    // Request the translation
    function curlRequest($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Type: text/xml");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, False);
        $curlResponse = curl_exec($ch);
        curl_close($ch);
        return $curlResponse;
    }
 
    public function getTranslation($fromLanguage, $toLanguage, $text) {
        $accessToken = $this->getToken($this->azure_key);
        $params = "text=" . urlencode($text) . "&to=" . $toLanguage . "&from=" . $fromLanguage . "&appId=Bearer+" . $accessToken;
        $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params";
        $curlResponse = $this->curlRequest($translateUrl);
        $translatedStr = simplexml_load_string($curlResponse);
        return $translatedStr;
    }
 
    
}

Please store this code under the name translate.class.php

The following code uses the class and provides the translation:

require_once('translate.class.php');
$BingTranslator = new BingTranslator();
$translation = $BingTranslator->getTranslation('en', 'de', 'What time does the hotel close in the evening?');
echo $translation;

Last edited by pkedpker (2018-04-17 22:32:04)

Offline

#2 2018-04-18 11:30:09

PeopleInside
Administrator
From: Italy
Registered: 2014-04-10
Posts: 4,046
Website

Re: Microsoft Translator has moved and now requires new API

Hi pkedpker, welcome on the live helper chat.
This is a code request - in this case please use GitHub -

I created an issue for you, you can subscribe https://github.com/LiveHelperChat/liveh … ssues/1212


lol PeopleInside - Live helper chat - free limited forum support!
wink For commercial support or GitHub [see FAQ here]
ops If you want to support this open source project, just donate [see support page]
glasses Something wrong with the forum? [contact a superhero]

Offline

#3 2018-04-27 18:52:10

pkedpker
Member
Registered: 2018-04-17
Posts: 2

Re: Microsoft Translator has moved and now requires new API

Thanks PeopleInside

Here is the Yandex api i found from github

use Yandex\Translate\Translator;
use Yandex\Translate\Exception;

try {
  $translator = new Translator($key);
  $translation = $translator->translate('Hello world', 'en-ru');

  echo $translation; // Привет мир

  echo $translation->getSource(); // Hello world;

  echo $translator->detect("Привет мир"); //ru

  echo $translation->getSourceLanguage(); // en
  echo $translation->getResultLanguage(); // ru
} catch (Exception $e) {
  // handle exception
}

Here are the classes
https://github.com/yandex-php/translate … master/src

Here is to get Yandex free API Key
http://api.yandex.com/key/form.xml?service=trnsl

Last edited by pkedpker (2018-04-27 18:53:51)

Offline

Board footer