微信公众平台消息接口开发(13)多语种互译
1 <?php 2 3 class AccessTokenAuthentication { 4 /* 5 * Get the access token. 6 * 7 * @param string $grantType Grant type. 8 * @param string $scopeUrl Application Scope URL. 9 * @param string $clientID Application client ID. 10 * @param string $clientSecret Application client ID. 11 * @param string $authUrl Oauth Url. 12 * 13 * @return string. 14 */ 15 function getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl){ 16 try { 17 //Initialize the Curl Session. 18 $ch = curl_init(); 19 //Create the request Array. 20 $paramArr = array ( 21 'grant_type' => $grantType, 22 'scope' => $scopeUrl, 23 'client_id' => $clientID, 24 'client_secret' => $clientSecret 25 ); 26 //Create an Http Query.// 27 $paramArr = http_build_query($paramArr); 28 //Set the Curl URL. 29 curl_setopt($ch, CURLOPT_URL, $authUrl); 30 //Set HTTP POST Request. 31 curl_setopt($ch, CURLOPT_POST, TRUE); 32 //Set data to POST in HTTP "POST" Operation. 33 curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr); 34 //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). 35 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); 36 //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. 37 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 38 //Execute the cURL session. 39 $strResponse = curl_exec($ch); 40 //Get the Error Code returned by Curl. 41 $curlErrno = curl_errno($ch); 42 if($curlErrno){ 43 $curlError = curl_error($ch); 44 throw new Exception($curlError); 45 } 46 //Close the Curl Session. 47 curl_close($ch); 48 //Decode the returned JSON string. 49 $objResponse = json_decode($strResponse); 50 if ($objResponse->error){ 51 throw new Exception($objResponse->error_description); 52 } 53 return $objResponse->access_token; 54 } catch (Exception $e) { 55 echo "Exception-".$e->getMessage(); 56 } 57 } 58 } 59 60 /* 61 * Class:HTTPTranslator 62 * 63 * Processing the translator request. 64 */ 65 Class HTTPTranslator { 66 /* 67 * Create and execute the HTTP CURL request. 68 * 69 * @param string $url HTTP Url. 70 * @param string $authHeader Authorization Header string. 71 * 72 * @return string. 73 * 74 */ 75 function curlRequest($url, $authHeader){ 76 //Initialize the Curl Session. 77 $ch = curl_init(); 78 //Set the Curl url. 79 curl_setopt ($ch, CURLOPT_URL, $url); 80 //Set the HTTP HEADER Fields. 81 curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader)); 82 //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). 83 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); 84 //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. 85 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False); 86 //Execute the cURL session. 87 $curlResponse = curl_exec($ch); 88 //Get the Error Code returned by Curl. 89 $curlErrno = curl_errno($ch); 90 if ($curlErrno) { 91 $curlError = curl_error($ch); 92 throw new Exception($curlError); 93 } 94 //Close a cURL session. 95 curl_close($ch); 96 return $curlResponse; 97 } 98 } 99 100 try { 101 //Client ID of the application. 102 $clientID = "clientId"; 103 //Client Secret key of the application. 104 $clientSecret = "ClientSecret"; 105 //OAuth Url. 106 $authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"; 107 //Application Scope Url 108 $scopeUrl = "http://api.microsofttranslator.com"; 109 //Application grant type 110 $grantType = "client_credentials"; 111 112 //Create the AccessTokenAuthentication object. 113 $authObj = new AccessTokenAuthentication(); 114 //Get the Access token. 115 $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl); 116 //Create the authorization Header string. 117 $authHeader = "Authorization: Bearer ". $accessToken; 118 119 //Set the params. 120 $sentence = "rephrasing is a hard problem for the computer."; 121 $language = "en-us"; 122 $category = "general"; 123 $maxParaphrase = '6'; 124 125 $params = "sentence=".urlencode($sentence)."&language=$language&category=$category&maxParaphrases=$maxParaphrase"; 126 127 //HTTP paraphrase URL. 128 $paraphraseUrl = "http://api.microsofttranslator.com/v3/json/paraphrase?$params"; 129 130 //Create the Translator Object. 131 $translatorObj = new HTTPTranslator(); 132 133 //Call the curlRequest. 134 echo $curlResponse = $translatorObj->curlRequest($paraphraseUrl, $authHeader); 135 136 } catch (Exception $e) { 137 echo "Exception: " . $e->getMessage() . PHP_EOL; 138 } 139 140 /* 141 * Create and execute the HTTP CURL request. 142 * 143 * @param string $url HTTP Url. 144 * @param string $authHeader Authorization Header string. 145 * @param string $postData Data to post. 146 * 147 * @return string. 148 * 149 */ 150 function curlRequest($url, $authHeader) { 151 //Initialize the Curl Session. 152 $ch = curl_init(); 153 //Set the Curl url. 154 curl_setopt ($ch, CURLOPT_URL, $url); 155 //Set the HTTP HEADER Fields. 156 curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader,"Content-Type: text/xml")); 157 //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). 158 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); 159 //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. 160 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False); 161 //Execute the cURL session. 162 $curlResponse = curl_exec($ch); 163 //Get the Error Code returned by Curl. 164 $curlErrno = curl_errno($ch); 165 if ($curlErrno) { 166 $curlError = curl_error($ch); 167 throw new Exception($curlError); 168 } 169 //Close a cURL session. 170 curl_close($ch); 171 return $curlResponse; 172 }