Created
April 25, 2019 17:09
-
-
Save caugner/337931f83752d128579c24f5a083eff1 to your computer and use it in GitHub Desktop.
deepl-translate-multiple-texts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use GuzzleHttp\Client as HttpClient; | |
use GuzzleHttp\Exception\GuzzleException; | |
require_once 'vendor/autoload.php'; | |
$input = ["Hallo", "Welt"]; | |
print_r($input); | |
try { | |
$httpClient = new HttpClient([ 'base_uri' => 'https://api.deepl.com/v2/']); | |
$response = $httpClient->request('POST', 'translate', [ | |
'form_params' => [ | |
'text' => $input, | |
'source_lang' => "DE", | |
'target_lang' => "EN", | |
'split_sentences' => 0, | |
'preserve_formatting' => 0, | |
'auth_key' => "***" | |
], | |
]); | |
$json = $response->getBody()->getContents(); | |
$result = json_decode($json, true); | |
$output = array_column($result['translations'], 'text'); | |
print_r($output); | |
} catch (GuzzleException $e) { | |
echo $e->getMessage(); | |
} |
Should be "text=Hallo&text=Welt"
look "following example illustrates how to send three texts to be translated at once:"
Its probably too late but if someone needs solution - json_encode should be used on array:
Example:
'form_params' => [
'auth_key' => '***',
'text' => json_encode($input),
'source_lang' => "EN",
'target_lang' => "DE",
'split_sentences' => 0,
'preserve_formatting' => 0,
],
and to get results in format you wanted:
$json = $response->getBody()->getContents();
$result = json_decode($json, true);
$output = array_column($result['translations'], 'text');
$expectedOutput = json_decode($output[0]);
@yemanjo thank you very much - with json it works :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected
Actual