Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 80a0fca

Browse files
send text as POST body to solve 414 uri too long error
1 parent 9e773dd commit 80a0fca

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

src/DeepL.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ public function translate($text, $sourceLanguage = 'de', $destinationLanguage =
131131
$this->checkLanguages($sourceLanguage, $destinationLanguage);
132132

133133
// build the DeepL API request url
134-
$url = $this->buildUrl($text, $sourceLanguage, $destinationLanguage);
134+
$url = $this->buildUrl($sourceLanguage, $destinationLanguage);
135+
$body = $this->buildBody($text);
135136

136137
// request the DeepL API
137-
$translationsArray = $this->request($url);
138+
$translationsArray = $this->request($url, $body);
138139
$translationsCount = count($translationsArray['translations']);
139140

140141
if ($translationsCount == 0) {
@@ -184,22 +185,42 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
184185
*
185186
* @return string
186187
*/
187-
protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
188+
protected function buildUrl($sourceLanguage, $destinationLanguage)
188189
{
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+
189210
if (!is_array($text)) {
190211
$text = (array)$text;
191212
}
192213

193-
$url = DeepL::API_URL . '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
194-
195214
foreach ($text as $textElement) {
196-
$url .= '&' . sprintf(DeepL::API_URL_TEXT, rawurlencode($textElement));
197-
}
198215

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));
201217

202-
return $url;
218+
if ($first) {
219+
$first = false;
220+
}
221+
}
222+
223+
return $body;
203224
}
204225

205226
/**
@@ -211,9 +232,12 @@ protected function buildUrl($text, $sourceLanguage, $destinationLanguage)
211232
*
212233
* @throws DeepLException
213234
*/
214-
protected function request($url)
235+
protected function request($url, $body)
215236
{
216237
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+
217241
$response = curl_exec($this->curl);
218242

219243
if (!curl_errno($this->curl)) {

tests/DeepLTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,51 @@ public function testCheckLanguagesDestinationLanguageException()
7676
*/
7777
public function testBuildUrl()
7878
{
79-
$expectedString = 'https://api.deepl.com/v1/translate?auth_key=123456&text=Hallo%20Welt&source_lang=de&target_lang=en';
79+
$expectedString = 'https://api.deepl.com/v1/translate?auth_key=123456&source_lang=de&target_lang=en';
8080

8181
$authKey = '123456';
8282
$deepl = new DeepL($authKey);
8383

8484
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
8585

86-
$return = $buildUrl->invokeArgs($deepl, array('Hallo Welt', 'de', 'en'));
86+
$return = $buildUrl->invokeArgs($deepl, array('de', 'en'));
8787

8888
$this->assertEquals($expectedString, $return);
8989
}
9090

9191
/**
92-
* Test translate()
92+
* Test buildBody()
93+
*/
94+
public function testBuildBody()
95+
{
96+
$expectedString = 'text=Hallo%20Welt';
97+
98+
$authKey = '123456';
99+
$deepl = new DeepL($authKey);
100+
101+
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildBody');
102+
103+
$return = $buildUrl->invokeArgs($deepl, array('Hallo Welt'));
104+
105+
$this->assertEquals($expectedString, $return);
106+
}
107+
108+
/**
109+
* Test translate() calls
110+
*/
111+
public function testTranslate()
112+
{
113+
$mock = $this->getMockBuilder('\BabyMarkt\DeepL\DeepL');
114+
115+
// TODO: test if translate methods calls correct methods
116+
}
117+
118+
/**
119+
* Test translate() success
93120
*
94121
* TEST REQUIRES VALID DEEPL AUTH KEY!!
95122
*/
96-
public function testTranslate()
123+
public function testTranslateSuccess()
97124
{
98125
return;
99126

0 commit comments

Comments
 (0)