groupdocs-free-consulting / projects

0 stars 0 forks source link

I'm developing a bilingual website and need help automating the translation in PHP #43

Open mcasto opened 8 months ago

mcasto commented 8 months ago

Using PHP, I am able to generate a token with :

$url = 'https://api.groupdocs.cloud/connect/token';
$data = array(
  'grant_type' => 'client_credentials',
  'client_id' => $clientId,
  'client_secret' => $clientSecret
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

If I take that token and use it in the sample code from the docs, it works fine in Terminal:

curl -X POST "https://api.groupdocs.cloud/v1.0/translation/text" \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d "'[{\"pair\": \"en-fr\", \"text\": \"Welcome to Paris\"}]'"

But, when I try to do the same with PHP, I get an error:

$data = json_encode([['pair' => 'en-fr', 'text' => 'Welcome to Paris']]);

$url = 'https://api.groupdocs.cloud/v1.0/translation/text';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: Bearer ' . $token,
  'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if ($response === false) {
  echo 'Curl error: ' . curl_error($ch);
}

curl_close($ch);

print_r(json_decode($response));

The error I get is:

 [error] => stdClass Object
        (
            [code] => errorInvalidInputData
            [message] => : Unexpected character encountered while parsing value: [. Path '', line 1, position 1.
            [description] => Operation Failed. The input data is not valid.
            [dateTime] => 2023-11-29T02:09:17.7678815Z
            [innerError] => 
        )

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.

tilalahmad commented 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.

tilalahmad commented 8 months ago

@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);
?>