<?php
class AccessTokenAuthentication {
/*
* Get the access token.
*
* @[USER=314738]param[/USER] string $grantType Grant type.
* @[USER=314738]param[/USER] string $scopeUrl Application Scope URL.
* @[USER=314738]param[/USER] string $clientID Application client ID.
* @[USER=314738]param[/USER] string $clientSecret Application client ID.
* @[USER=314738]param[/USER] string $authUrl Oauth Url.
*
* @[USER=273296]Return[/USER] string.
*/
function getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl){
try {
//Initialize the Curl Session.
$ch = curl_init();
//Create the request Array.
$paramArr = array (
'grant_type' => $grantType,
'scope' => $scopeUrl,
'client_id' => $clientID,
'client_secret' => $clientSecret
);
//Create an Http Query.//
$paramArr = http_build_query($paramArr);
//Set the Curl URL.
curl_setopt($ch, CURLOPT_URL, $authUrl);
//Set HTTP POST Request.
curl_setopt($ch, CURLOPT_POST, TRUE);
//Set data to POST in HTTP "POST" Operation.
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr);
//CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
//CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Execute the cURL session.
$strResponse = curl_exec($ch);
//Get the Error Code returned by Curl.
$curlErrno = curl_errno($ch);
if($curlErrno){
$curlError = curl_error($ch);
throw new Exception($curlError);
}
//Close the Curl Session.
curl_close($ch);
//Decode the returned JSON string.
$objResponse = json_decode($strResponse);
if ($objResponse->error){
throw new Exception($objResponse->error_description);
}
return $objResponse->access_token;
} catch (Exception $e) {
echo "Exception-".$e->getMessage();
}
}
}
Class HTTPTranslator {
/*
* Create and execute the HTTP CURL request.
*
* @[USER=314738]param[/USER] string $url HTTP Url.
* @[USER=314738]param[/USER] string $authHeader Authorization Header string.
* @[USER=314738]param[/USER] string $postData Data to post.
*
* @[USER=273296]Return[/USER] string.
*
*/
function curlRequest($url, $authHeader, $postData=''){
//Initialize the Curl Session.
$ch = curl_init();
//Set the Curl url.
curl_setopt ($ch, CURLOPT_URL, $url);
//Set the HTTP HEADER Fields.
curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader,"Content-Type: text/xml"));
//CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
//CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False);
if($postData) {
//Set HTTP POST Request.
curl_setopt($ch, CURLOPT_POST, TRUE);
//Set data to POST in HTTP "POST" Operation.
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
//Execute the cURL session.
$curlResponse = curl_exec($ch);
//Get the Error Code returned by Curl.
$curlErrno = curl_errno($ch);
if ($curlErrno) {
$curlError = curl_error($ch);
throw new Exception($curlError);
}
//Close a cURL session.
curl_close($ch);
return $curlResponse;
}
/*
* Create Request XML Format.
*
* @[USER=314738]param[/USER] string $fromLanguage Source language Code.
* @[USER=314738]param[/USER] string $toLanguage Target language Code.
* @[USER=314738]param[/USER] string $contentType Content Type.
* @[USER=314738]param[/USER] string $inputStrArr Input String Array.
*
* @[USER=273296]Return[/USER] string.
*/
function createReqXML($fromLanguage,$toLanguage,$contentType,$inputStrArr) {
//Create the XML string for passing the values.
$requestXml = "<TranslateArrayRequest>".
"<AppId/>".
"<From>$fromLanguage</From>".
"<Options>" .
"<Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" .
"<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">$contentType</ContentType>" .
"<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" .
"<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" .
"<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" .
"<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" .
"</Options>" .
"<Texts>";
foreach ($inputStrArr as $inputStr)
$requestXml .= "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">$inputStr</string>" ;
$requestXml .= "</Texts>".
"<To>$toLanguage</To>" .
"</TranslateArrayRequest>";
return $requestXml;
}
}
try {
//Client ID of the application.
$clientID = "clientId";
//Client Secret key of the application.
$clientSecret = "clientSecret";
//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 = "de";
$inputStrArr = array("The answer lies in machine translation.", "the best machine translation technology cannot always provide translations tailored to a site or users like a human.");
$contentType = 'text/plain';
//Create the Translator Object.
$translatorObj = new HTTPTranslator();
//Get the Request XML Format.
$requestXml = $translatorObj->createReqXML($fromLanguage,$toLanguage,$contentType,$inputStrArr);
//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);
$i=0;
echo "<table border=2px>";
echo "<tr>";
echo "<td><b>From $fromLanguage</b></td><td><b>To $toLanguage</b></td>";
echo "</tr>";
foreach($xmlObj->TranslateArrayResponse as $translatedArrObj){
echo "<tr><td>".$inputStrArr[$i]."</td><td>". $translatedArrObj->TranslatedText."</td></tr>";
$i++;
}
echo "</table>";
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . PHP_EOL;
}