Advertisement
Gio88

Bing Translator Code

Jul 18th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. {
  2. $clientID = "XXX";
  3. //Client Secret key of the application.
  4. $clientSecret = "XXX";
  5. //OAuth Url.
  6. $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
  7. //Application Scope Url
  8. $scopeUrl = "http://api.microsofttranslator.com";
  9. //Application grant type
  10. $grantType = "client_credentials";
  11. // Create the AccessTokenAuthentication object.
  12. $authObj = new AccessTokenAuthentication();
  13. // Get the Access token.
  14. $accessToken = $authObj -> getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl);
  15. // Create the authorization Header string.
  16. $authHeader = "Authorization: Bearer ". $accessToken;
  17. // Set the parameters.
  18. // Sets source language. $fromLanguage = variable, langs[source][0] = name of textarea.
  19. $fromLanguage =$_COOKIE['cookie_source'] ;
  20. // Sets destination language. $toLanguage = variable, dest_lang = name of textarea.
  21. $toLanguage = $_COOKIE['cookie_dest'];
  22. // Sets text to translate. $inputStr = variable, source_text = content of thextarea.
  23. $inputStr = $_COOKIE['cookie_final'];
  24. echo "<script type='text/javascript'>alert('$inputStr');</script>";
  25. $contentType = 'text/plain';
  26. $category = 'general';
  27. // Variable that composes the string of parameters for the transaltion
  28. $paramst = "text=".urlencode($inputStr)."&to=".$toLanguage."&from=".$fromLanguage;
  29. // URL to translate the text
  30. $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$paramst";
  31. //Create the Translator Object.
  32. $translatorObj = new HTTPTranslator();
  33. //Get the curlResponse.
  34. $curlResponse = $translatorObj -> curlRequest($translateUrl, $authHeader);
  35. //Interprets a string of XML into an object.
  36. $xmlObj = simplexml_load_string($curlResponse);
  37. foreach((array)$xmlObj[0] as $val)
  38. {
  39. $translatedStr = $val;
  40. }
  41. $scrivi=utf8_decode($translatedStr);
  42. $translatedText = urlencode($translatedStr);
  43. $scrivi2=utf8_decode($translatedText);
  44. if (isset($inputStr)== true){
  45. echo "<script type='text/javascript'>alert('e piena');</script>";
  46. echo "<script type='text/javascript'>alert(.$translatedText);</script>";
  47. echo "<script type='text/javascript'>alert('.$translatedText');</script>";
  48. echo "<script type='text/javascript'>alert('.$translatedStr');</script>";
  49. echo "<script type='text/javascript'>alert(.$translatedStr);</script>";
  50. }
  51. }
  52. catch (Exception $e)
  53. {
  54. echo "Exception: ".$e->getMessage().PHP_EOL;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement