@@ -131,10 +131,11 @@ public function translate($text, $sourceLanguage = 'de', $destinationLanguage =
131
131
$ this ->checkLanguages ($ sourceLanguage , $ destinationLanguage );
132
132
133
133
// build the DeepL API request url
134
- $ url = $ this ->buildUrl ($ text , $ sourceLanguage , $ destinationLanguage );
134
+ $ url = $ this ->buildUrl ($ sourceLanguage , $ destinationLanguage );
135
+ $ body = $ this ->buildBody ($ text );
135
136
136
137
// request the DeepL API
137
- $ translationsArray = $ this ->request ($ url );
138
+ $ translationsArray = $ this ->request ($ url, $ body );
138
139
$ translationsCount = count ($ translationsArray ['translations ' ]);
139
140
140
141
if ($ translationsCount == 0 ) {
@@ -184,22 +185,42 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
184
185
*
185
186
* @return string
186
187
*/
187
- protected function buildUrl ($ text , $ sourceLanguage , $ destinationLanguage )
188
+ protected function buildUrl ($ sourceLanguage , $ destinationLanguage )
188
189
{
190
+ $ url = DeepL::API_URL . '? ' . sprintf (DeepL::API_URL_AUTH_KEY , $ this ->authKey );
191
+
192
+ $ url .= '& ' . sprintf (DeepL::API_URL_SOURCE_LANG , strtolower ($ sourceLanguage ));
193
+ $ url .= '& ' . sprintf (DeepL::API_URL_DESTINATION_LANG , strtolower ($ destinationLanguage ));
194
+
195
+ return $ url ;
196
+ }
197
+
198
+ /**
199
+ * Build the body for the DeepL API request
200
+ *
201
+ * @param $text
202
+ *
203
+ * @return string
204
+ */
205
+ protected function buildBody ($ text )
206
+ {
207
+ $ body = '' ;
208
+ $ first = true ;
209
+
189
210
if (!is_array ($ text )) {
190
211
$ text = (array )$ text ;
191
212
}
192
213
193
- $ url = DeepL::API_URL . '? ' . sprintf (DeepL::API_URL_AUTH_KEY , $ this ->authKey );
194
-
195
214
foreach ($ text as $ textElement ) {
196
- $ url .= '& ' . sprintf (DeepL::API_URL_TEXT , rawurlencode ($ textElement ));
197
- }
198
215
199
- $ url .= '& ' . sprintf (DeepL::API_URL_SOURCE_LANG , strtolower ($ sourceLanguage ));
200
- $ url .= '& ' . sprintf (DeepL::API_URL_DESTINATION_LANG , strtolower ($ destinationLanguage ));
216
+ $ body .= ($ first ? '' : '& ' ) . sprintf (DeepL::API_URL_TEXT , rawurlencode ($ textElement ));
201
217
202
- return $ url ;
218
+ if ($ first ) {
219
+ $ first = false ;
220
+ }
221
+ }
222
+
223
+ return $ body ;
203
224
}
204
225
205
226
/**
@@ -211,9 +232,12 @@ protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
211
232
*
212
233
* @throws DeepLException
213
234
*/
214
- protected function request ($ url )
235
+ protected function request ($ url, $ body )
215
236
{
216
237
curl_setopt ($ this ->curl , CURLOPT_URL , $ url );
238
+ curl_setopt ($ this ->curl , CURLOPT_POSTFIELDS , $ body );
239
+ curl_setopt ($ this ->curl , CURLOPT_HTTPHEADER , array ('Content-Type: application/x-www-form-urlencoded ' ));
240
+
217
241
$ response = curl_exec ($ this ->curl );
218
242
219
243
if (!curl_errno ($ this ->curl )) {
0 commit comments