-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-translator.php
More file actions
93 lines (74 loc) · 3.01 KB
/
php-translator.php
File metadata and controls
93 lines (74 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* Author: Name
* Created: 5.06.2016
*
**/
class PHPTranslator {
public $clients = [];
private $_debug = false;
public function addClient($id, $secret) {
$this->clients[] = ["id"=>$id, "secret"=>$secret];
}
public function debug($value){
$this->_debug = $value;
}
private function getAccesToken ($clientID, $clientSecret){
$authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
$scopeUrl = "http://api.microsofttranslator.com";
$grantType = "client_credentials";
$paramArr = array (
'grant_type' => $grantType,
'scope' => $scopeUrl,
'client_id' => $clientID,
'client_secret' => $clientSecret
);
$paramArr = http_build_query($paramArr);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authUrl);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$strResponse = curl_exec($ch);
$curlErrno = curl_errno($ch);
if($curlErrno){
$curlError = curl_error($ch);
throw new Exception($curlError);
}
curl_close($ch);
$objResponse = json_decode($strResponse);
if($this->_debug) var_dump($objResponse);
return $objResponse->access_token;
}
private function curlRequest($url, $authHeader, $postData=''){
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader,"Content-Type: text/xml"));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False);
if($postData) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
$curlResponse = curl_exec($ch);
$curlErrno = curl_errno($ch);
if ($curlErrno) {
$curlError = curl_error($ch);
throw new Exception($curlError);
}
curl_close($ch);
return $curlResponse;
}
public function translate($text, $to="en", $from="auto") {
$client = $this->clients[rand(0,count($this->clients)-1)];
$accessToken = $this->getAccesToken($client["id"], $client["secret"]);
$authHeader = "Authorization: Bearer ". $accessToken;
$requestUrl = "http://api.microsofttranslator.com/V2/Http.svc/Translate?text=".urlencode($text)."&to=$to";
if($from!="auto")
$requestUrl.="&from=$from";
$strResponse = $this->curlRequest($requestUrl, $authHeader);
$xmlObj = simplexml_load_string($strResponse);
return $xmlObj[0][0];
}
}