📜 ⬆️ ⬇️

Using the Microsoft Bing Translator API in Your Application

Good day!

Sometimes there is a need to translate data entered by the user on the site (to generate CNC on sites, with multilingual content, etc.). Since the Google Translate API is currently only available for money, the Bing Translator API can be used as a solution.

Microsoft's official website has instructions for use ( http://msdn.microsoft.com/en-us/library/dd576287.aspx , http://www.microsoft.com/web/post/using-the-free-bing -translation-apis ), but, in my opinion, they are quite confusing and verbose, I will describe a simple algorithm for connecting and using the service.

To use Microsoft's services, you usually need Windows Live Id. Previously, to use the Bing Translator API, it was enough to get the Bing App ID of the application by registering it at https://ssl.bing.com/webmaster/developers/appids.aspx . Now this mechanism is considered obsolete and is no longer supported .
')
At this point, to use the translator API in your application, you need to do the following.

1. Get Windows Live Id (by registering at https://accountservices.passport.net/reg.srf )

2. Log in to Live Id at datamarket.azure.com and register there, then select Microsoft Data in the Data Translator section. There will be a list of tariffs on the right, we need the lowest one - free, we subscribe to it (Sign up).

3. Go to the "application" section, register a new application. Client ID is filled randomly, Client secret is already generated. Redirect URI specify any, it is not used.

4. Now you can start using. The general concept of working with the API is as follows. A client accesses https to Microsoft's OAuth service, passing its client_id and client_secret in a POST request. A json-serialized object is returned in response, which has an access_token field. The token is valid for 10 minutes, then you need to re-request it.
After the access_token is received, you can send a GET request to the translator by signing a token. One way of signing is to pass the getIt parameter appId = Bearer TOCEN VALUE (along with a space). In the same gett request the data that needs to be transferred is transmitted. If successful, the answer is the translated string.

I will give an example of a php-class that translates text.

<? php
class translate {

protected $ msData , $ accessToken ;

public function __construct ( $ msData ) { // client_id and client_secret are stored in the msdata array
$ this -> msData = $ msData ;
$ this -> initAccessToken ( ) ;
}

protected function initAccessToken ( ) { // get accessToken
$ curl = curl_init ( " datamarket.accesscontrol.windows.net/v2/OAuth2-13/" ) ;
curl_setopt_array ( $ curl , array (
CURLOPT_POST => true , // create a POST request to get a token
CURLOPT_POSTFIELDS => http_build_query ( array (
'client_id' => $ this -> msData [ 'clientid' ] ,
'client_secret' => $ this -> msData [ 'clientsecret' ] ,
'scope' => 'http://api.microsofttranslator.com' , // this value is indicated in the documentation
'grant_type' => "client_credentials" // this value is indicated in the documentation
) )
CURLOPT_SSL_VERIFYPEER => false ,
CURLOPT_RETURNTRANSFER => true ,
) )
$ response = curl_exec ( $ curl ) ;
$ err = curl_errno ( $ curl ) ;
if ( $ err )
throw new Exception ( "curl err $ err " ) ;
$ r_obj = json_decode ( $ response ) ;
if ( ! isset ( $ r_obj ) )
throw new Exception ( "illegal response: $ response " ) ;
$ this -> accessToken = $ r_obj -> access_token ;
}

public function translate ( $ text , $ lang_to , $ lang_from ) {
$ query_arr = array (
'appId' => 'Bearer' . $ this -> accessToken ,
'text' => $ text ,
'from' => $ lang_from ,
'to' => $ lang_to ,
'contentType' => 'text / plain'
) ;
$ query = http_build_query ( $ query_arr ) ;
$ url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?' . $ query ;
$ curl = curl_init ( $ url ) ;
curl_setopt ( $ curl , CURLOPT_RETURNTRANSFER , 1 ) ;
$ response = curl_exec ( $ curl ) ;
$ err = curl_errno ( $ curl ) ;
if ( $ err )
throw new Exception ( "curl err $ err " ) ;
$ xml = new SimpleXMLElement ( $ response ) ; // get the translation string from the xml-response
return ( string ) $ xml ;
}
}


I hope this post will be useful to those who decide to connect the online translator to their application.

Source: https://habr.com/ru/post/143323/


All Articles