2
2
3
3
namespace BabyMarkt \DeepL ;
4
4
5
+ /**
6
+ * DeepL API client library
7
+ *
8
+ * @package BabyMarkt\DeepL
9
+ */
5
10
class DeepL
6
11
{
7
12
/**
8
- * API URL
13
+ * API v1 URL
9
14
*/
10
15
const API_URL = 'https://api.deepl.com/v1/translate ' ;
11
16
12
17
/**
13
- * API URL: Parameter auth_key
18
+ * API v1 URL: Parameter auth_key
14
19
*/
15
20
const API_URL_AUTH_KEY = 'auth_key=%s ' ;
16
21
17
22
/**
18
- * API URL: Parameter text
23
+ * API v1 URL: Parameter text
19
24
*/
20
25
const API_URL_TEXT = 'text=%s ' ;
21
26
22
27
/**
23
- * API URL: Parameter source_lang
28
+ * API v1 URL: Parameter source_lang
24
29
*/
25
30
const API_URL_SOURCE_LANG = 'source_lang=%s ' ;
26
31
27
32
/**
28
- * API URL: Parameter target_lang
33
+ * API v1 URL: Parameter target_lang
29
34
*/
30
35
const API_URL_DESTINATION_LANG = 'target_lang=%s ' ;
31
36
@@ -66,6 +71,13 @@ class DeepL
66
71
*/
67
72
protected $ authKey ;
68
73
74
+ /**
75
+ * cURL resource
76
+ *
77
+ * @var resource
78
+ */
79
+ protected $ curl ;
80
+
69
81
/**
70
82
* DeepL constructor
71
83
*
@@ -74,18 +86,33 @@ class DeepL
74
86
public function __construct ($ authKey )
75
87
{
76
88
$ this ->authKey = $ authKey ;
89
+ $ this ->curl = curl_init ();
90
+
91
+ curl_setopt ($ this ->curl , CURLOPT_RETURNTRANSFER , 1 );
92
+ }
93
+
94
+ /**
95
+ * DeepL Destructor
96
+ */
97
+ public function __destruct ()
98
+ {
99
+ if ($ this ->curl && is_resource ($ this ->curl )) {
100
+ curl_close ($ this ->curl );
101
+ }
77
102
}
78
103
79
104
/**
80
105
* Translate the text string or array from source to destination language
81
106
*
82
- * @param $text
83
- * @param $sourceLanguage
84
- * @param $destinationLanguage
107
+ * @param $text string|string[]
108
+ * @param $sourceLanguage string
109
+ * @param $destinationLanguage string
85
110
*
86
111
* @return array
112
+ *
113
+ * @throws DeepLException
87
114
*/
88
- public function translate ($ text , $ sourceLanguage , $ destinationLanguage )
115
+ public function translate ($ text , $ sourceLanguage = ' de ' , $ destinationLanguage = ' en ' )
89
116
{
90
117
// make sure we only accept supported languages
91
118
$ this ->checkLanguages ($ sourceLanguage , $ destinationLanguage );
@@ -154,29 +181,30 @@ protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
154
181
*
155
182
* @param $url
156
183
*
157
- * @return mixed
184
+ * @return array
158
185
*
159
186
* @throws DeepLException
160
187
*/
161
188
protected function request ($ url )
162
189
{
163
- $ ch = curl_init ();
164
- curl_setopt ($ ch , CURLOPT_URL , $ url );
165
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
166
- $ output = curl_exec ($ ch );
167
- curl_close ($ ch );
190
+ curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
191
+ $ response = curl_exec ($ this ->curl );
168
192
169
193
// if there was a curl error
170
- if ($ output == false ) {
194
+ if ($ response == false ) {
195
+
196
+ $ errorString = curl_error ($ this ->curl );
197
+ $ errorNumber = curl_errno ($ this ->curl );
171
198
172
- $ errorString = curl_error ($ ch );
173
- $ errorNumber = curl_errno ($ ch );
199
+ if (!$ errorString ) {
200
+ $ errorString = 'There was a cURL Response Error - please check your DeepL Auth Key. ' ;
201
+ }
174
202
175
203
throw new DeepLException ($ errorString , $ errorNumber );
176
204
}
177
205
178
- $ json = json_decode ($ output );
206
+ $ translationsArray = json_decode ($ response , true );
179
207
180
- return $ json ;
208
+ return $ translationsArray ;
181
209
}
182
210
}
0 commit comments