Using the Microsoft Bing Translator API in your application
Good afternoon!
Sometimes there is a need to translate the data entered by the user on the site (for generating 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 to solve the problem.
The official Microsoft 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 services, you usually need a 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 the moment, 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 Translator in the Data section. On the right there will be a list of tariffs, we need the lowest-free one, subscribe to it (Sign up).
3. Go to the "applications" section , register a new application. We fill in the Client ID arbitrarily, the Client secret has already been generated. Redirect URI specify any, it is not used.
4. Now you can start using it. The general concept of working with the API is as follows. The client uses https to access the Microsoft OAuth service, passing its client_id and client_secret in a POST request. The response returns a json-serialized object with an access_token field in it. The token is valid for 10 minutes, then you need to request it again.
After the access_token is received, you can send a GET request to the translator by signing with the token. One way to sign is to pass the getIt parameter appId = Bearer TOKEN_VALUE (along with a space). In the same get request, data that needs to be transferred is transmitted. If successful, the response is the translated string.
I will give an example of a php class that translates text.
I hope this post will be useful to those who decide to connect an online translator to their application.
Sometimes there is a need to translate the data entered by the user on the site (for generating 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 to solve the problem.
The official Microsoft 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 services, you usually need a 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 the moment, 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 Translator in the Data section. On the right there will be a list of tariffs, we need the lowest-free one, subscribe to it (Sign up).
3. Go to the "applications" section , register a new application. We fill in the Client ID arbitrarily, the Client secret has already been generated. Redirect URI specify any, it is not used.
4. Now you can start using it. The general concept of working with the API is as follows. The client uses https to access the Microsoft OAuth service, passing its client_id and client_secret in a POST request. The response returns a json-serialized object with an access_token field in it. The token is valid for 10 minutes, then you need to request it again.
After the access_token is received, you can send a GET request to the translator by signing with the token. One way to sign is to pass the getIt parameter appId = Bearer TOKEN_VALUE (along with a space). In the same get request, data that needs to be transferred is transmitted. If successful, the response is the translated string.
I will give an example of a php class that translates text.
class Translate {
protected $ msData, $ accessToken;
public function __construct ($ msData) {// client_id and client_secret
$ this-> msData = $ msData are stored in the msdata array;
$ 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, // generate a POST request to receive the 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 specified 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 ("
$ 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);
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 an online translator to their application.