Open mcasto opened 8 months ago
@mcasto
We are sorry for the inconvenience. I have noticed the reported issue and logged a ticket (GDTRANS-2737) for further investigation and rectification. We will notify you as soon as we resolve the issue.
@mcasto Thanks for your patience. Please note that the v1.0 Translation API will be closed very soon. I prepared a sample PHP script on how to use the v2.0 API with PHP. Hopefully, it will help you accomplish the task. Please feel free to contact us via our free support forum for any questions or suggestions.
<?php
// provide your valid credentials
$credentials = [
"grant_type" => "client_credentials",
"client_id" => "",
"client_secret" => ""
];
$sourceLanguage = "en";
$targetLanguage = "de";
$srcText = "Hello, world! I can read this text in my language.";
$data = array(
"sourceLanguage" => "{$sourceLanguage}",
"targetLanguages" => array("{$targetLanguage}"),
"texts" => array("{$srcText}")
);
$data_json = json_encode($data);
echo "getting access token\n";
$auth_request = curl_init();
curl_setopt_array($auth_request, array(
CURLOPT_URL => "https://id.groupdocs.cloud/connect/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded"
),
CURLOPT_POSTFIELDS => http_build_query($credentials)
));
$auth_response = curl_exec($auth_request);
$auth_json = json_decode($auth_response);
$token = "{$auth_json->token_type} {$auth_json->access_token}";
curl_close($auth_request);
echo "getting task id\n";
$post_request = curl_init();
curl_setopt_array($post_request, array(
CURLOPT_URL => "https://api.groupdocs.cloud/v2.0/translation/text",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: {$token}"
),
));
$post_response = curl_exec($post_request);
$post_json = json_decode($post_response);
$id = $post_json->id;
curl_close($post_request);
echo "getting translation result\n";
$get_request = curl_init();
curl_setopt_array($get_request, array(
CURLOPT_URL => "https://api.groupdocs.cloud/v2.0/translation/text/{$id}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: {$token}"
),
));
while (true) {
$get_response = curl_exec($get_request);
$get_json = json_decode($get_response);
if ($get_json->status == 200) {
echo "Translation:\n";
echo $get_json->translations->$targetLanguage[0];
break;
} else {
echo "Waiting for translation\n";
sleep(1);
}
}
curl_close($get_request);
?>
Using PHP, I am able to generate a token with :
If I take that token and use it in the sample code from the docs, it works fine in Terminal:
But, when I try to do the same with PHP, I get an error:
The error I get is:
My objective is to be able to run the English HTML files through the API to produce their equivalent in Spanish. I'll only need to run the API once for each file, and then if I make any updates, I'll run it again. Can you give me a working PHP script to accomplish the example I tried? If I have that, then I should be able to handle the rest.
Thanks.