因爲部分業務使用的微軟的bing的翻譯
API:http://api.microsofttranslator.com/V2/Ajax.svc/Translate
sample:
http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=mycallback1412835995529&appId=A4D660A48A6A97CCA791C34935E4C02BBB1BEC1C&from=&to=zh-cn&text=Guten%20Abend
因爲appID數量收到限制,我們需要自己申請一個appID,過程如下:
step1:
打開 https://datamarket.azure.com/dataset/bing/microsofttranslator 點擊活動訂閱下面的購買
step2:
打開 https://datamarket.azure.com/developer/applications/register 註冊app 填寫 client_id
得到你的 客戶端ID: hellowodbaby , 客戶端祕鑰:Pc2PqzjqxdeaZYb5KDrKdouzN7j8s5At5BBFXBRJoU0= .
打開 https://datamarket.azure.com/developer/applications 你會看到你註冊的
/*
* Get the access token.
*
* @param string $grantType Grant type.
* @param string $scopeUrl Application Scope URL.
* @param string $clientID Application client ID.
* @param string $clientSecret Application client ID.
* @param string $authUrl Oauth Url.
*
* @return 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,true);
var_dump($objResponse);
if (isset($objResponse['error'])){
throw new Exception($objResponse['error_description']);
return FALSE;
}
return $objResponse['access_token'];
} catch (Exception $e) {
echo "Exception-".$e->getMessage();
return FALSE;
}
}
//Client ID of the application.
$clientID = "hellowodbaby";
//Client Secret key of the application.
$clientSecret = "Pc2PqzjqxdeaZYb5KDrKdouzN7j8s5At5BBFXBRJoU0=";
//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";
$authObj = new AccessTokenAuthentication();
//Get the Access token.
$accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl);
var_dump("bearer%20".urlencode($accessToken) );
//上面輸出的就是 appid,這個appid每隔expires_in秒會變掉【在數組$objResponse 】,注意。
參考
http://msdn.microsoft.com/en-us/library/ff512385.aspx
http://msdn.microsoft.com/en-us/library/hh454950.aspx