Stichoza / google-translate-php

🔤 Free Google Translate API PHP Package. Translates totally free of charge.
MIT License
1.79k stars 380 forks source link

400 Bad Request Client error: GET #196

Closed richardsonoge closed 9 months ago

richardsonoge commented 10 months ago

I had this problem when I tried to translate my two-page PDF file. The first page translated fine, but the second page gave me this error:

[24-Oct-2023 22:16:22 America/New_York] PHP Fatal error:  Uncaught Stichoza\GoogleTranslate\Exceptions\TranslationRequestException: Client error: `GET https://translate.google.com/translate_a/single?client=gtx&hl=en&dt=t&dt=
bd&dt=at&dt=ex&dt=ld&dt=md&dt=qca&dt=rw
&dt=rm&dt=ss&sl=en&tl=fr&q=The+information+requested+on+this+form
+is+collected+under+the+Immigra61.602243` resulted in a `400 Bad Request` response:
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, w (truncated...)
 in /opt/lampp/htdocs/vendor/stichoza/google-translate-php/src/GoogleTranslate.php:326
Stack trace:
#0 /opt/lampp/htdocs/vendor/stichoza/google-translate-php/src/GoogleTranslate.php(247): Stichoza\GoogleTranslate\GoogleTranslate->getResponse()
#1 /opt/lampp/htdocs/src/translate.php(81): Stichoza\GoogleTranslate\GoogleTranslate->translate()
#2 /opt/lampp/htdocs/includes/functions.php(47): translate()
#3 /opt/lampp/htdocs/translatepdf.php(112): translateAllFilesTxt()
#4 {main}
  thrown in /opt/lampp/htdocs/vendor/stichoza/google-translate-php/src/GoogleTranslate.php on line 326

What do I have to do? Any help would be appreciated.

Stichoza commented 10 months ago

There is a limit of string length that you can translate. It's usually 5000 characters, but depends on the input. Can you try splitting text into smaller chunks?

richardsonoge commented 10 months ago

There is a limit of string length that you can translate. It's usually 5000 characters, but depends on the input. Can you try splitting text into smaller chunks?

I've done it but it keeps giving me the same error. What should I do in this case? I tried to translate from English to French.

Stichoza commented 10 months ago

Can you paste your code fragment here? And possibly the content of PDF file also, via Pastebin or something like that (if the file is not private or sensitive)

richardsonoge commented 10 months ago

Can you paste your code fragment here? And possibly the content of PDF file also, via Pastebin or something like that (if the file is not private or sensitive)

Here's the code for my translateAllFilesTxt function:

define('NUMBER_CHARACTERS_GOOGLE', '3700'); // 3731

if (!function_exists('translateAllFilesTxt')) {

    function translateAllFilesTxt($arrayListFilesTextOriginal, $outputTranslatePath, $commandTranslate, $from, $to) {

        $txtFilesTranslatePath = []; // Initialize an array to store paths of translated text files

        foreach ($arrayListFilesTextOriginal as $txtFile) {
            // Read the content of the original text file
            $txtContent = file_get_contents($txtFile);

            // singlebyte strings
            $stringSplit = str_split($txtContent, NUMBER_CHARACTERS_GOOGLE);
            $countStringSplit = count($stringSplit);

            $retreiveTheContenttranslate = '';

            for ($i=0; $i < $countStringSplit; $i++) { 

                $getStringSplit = $stringSplit[$i];
                // Translate the content from the detected language to the target language
                $translateText = translate($txtContent, $from, $to);

                if ($getStringSplit != $translateText) {

                    $retreiveTheContenttranslate .= $translateText;

                } else {

                    $retreiveTheContenttranslate .= $getStringSplit;

                }
            }

            // Extract the filename without extension
            $filenameWithoutExtension = pathinfo($txtFile, PATHINFO_FILENAME);
            $filenameOriginal = $commandTranslate . $filenameWithoutExtension . '.txt';
            $outputTranslateFilePath = $outputTranslatePath.'/'.$filenameOriginal;

            // Create the translation output file if it doesn't exist
            if (!file_exists($outputTranslateFilePath)) {
                fopen($outputTranslateFilePath, "w");
            }

            // Write the translated content to the translation output file
            if (file_put_contents($outputTranslateFilePath, $retreiveTheContenttranslate)) {
                $txtFilesTranslatePath[] = $outputTranslateFilePath; // Add the path to the translated text file to the array
            }
        }

        return $txtFilesTranslatePath; // Return an array of paths to created translated text files
    }
}

This is the text in my TXT file that I want translated:

The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.
The primary purpose for providing the requested information on this form is to report a change of address. Except for
The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment
DHS may share the information you provide on this form with other Federal, state, local, and foreign government
www.dhs.gov/privacy
www.uscis.gov/addresschange
Form AR-11 Edition 11/02/22 Page 2 of 2DHS Privacy NoticeAUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you provide to contact you about the immigration benefit you are seeking.DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or other immigration benefits.ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest of national security. All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS) uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal, state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal from the United States.NOTE: This form is not evidence of identity, age, or status claimed. Address Change Information and InstructionsU.S. Department of Homeland Security Citizenship and Immigration Services Attn: Change of Address 1344 Pleasants Drive Harrisonburg, VA 22801InstructionsComplete all fields on this form, sign and date the form, and mail it to:IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2DHS Privacy NoticeAUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you provide to contact you about the immigration benefit you are seeking.DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or other immigration benefits.ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest of national security. All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS) uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal, state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal from the United States.NOTE: This form is not evidence of identity, age, or status claimed. Address Change Information and InstructionsU.S. Department of Homeland Security Citizenship and Immigration Services Attn: Change of Address 1344 Pleasants Drive Harrisonburg, VA 22801InstructionsComplete all fields on this form, sign and date the form, and mail it to:IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2DHS Privacy NoticeAUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you provide to contact you about the immigration benefit you are seeking.DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or other immigration benefits.ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest of national security. All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS) uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal, state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal from the United States.NOTE: This form is not evidence of identity, age, or status claimed. Address Change Information and InstructionsU.S. Department of Homeland Security Citizenship and Immigration Services Attn: Change of Address 1344 Pleasants Drive Harrisonburg, VA 22801InstructionsComplete all fields on this form, sign and date the form, and mail it to:IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2
DHS Privacy Notice
AUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.
PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for
those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you
provide to contact you about the immigration benefit you are seeking.
DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment
and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or
other immigration benefits.
ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government
agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records
notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information
System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can
find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest
of national security.
All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For
detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is
required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS)
uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal,
state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal
from the United States.
NOTE: This form is not evidence of identity, age, or status claimed.
Address Change Information and Instructions
U.S. Department of Homeland Security
Citizenship and Immigration Services
Attn: Change of Address
1344 Pleasants Drive
Harrisonburg, VA 22801
Instructions
Complete all fields on this form, sign and date the form, and mail it to:
IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address
changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2DHS Privacy NoticeAUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you provide to contact you about the immigration benefit you are seeking.DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or other immigration benefits.ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest of national security. All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS) uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal, state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal from the United States.NOTE: This form is not evidence of identity, age, or status claimed. Address Change Information and InstructionsU.S. Department of Homeland Security Citizenship and Immigration Services Attn: Change of Address 1344 Pleasants Drive Harrisonburg, VA 22801InstructionsComplete all fields on this form, sign and date the form, and mail it to:IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2DHS Privacy NoticeAUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you provide to contact you about the immigration benefit you are seeking.DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or other immigration benefits.ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest of national security. All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS) uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal, state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal from the United States.NOTE: This form is not evidence of identity, age, or status claimed. Address Change Information and InstructionsU.S. Department of Homeland Security Citizenship and Immigration Services Attn: Change of Address 1344 Pleasants Drive Harrisonburg, VA 22801InstructionsComplete all fields on this form, sign and date the form, and mail it to:IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2DHS Privacy NoticeAUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you provide to contact you about the immigration benefit you are seeking.DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or other immigration benefits.ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest of national security. All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS) uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal, state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal from the United States.NOTE: This form is not evidence of identity, age, or status claimed. Address Change Information and InstructionsU.S. Department of Homeland Security Citizenship and Immigration Services Attn: Change of Address 1344 Pleasants Drive Harrisonburg, VA 22801InstructionsComplete all fields on this form, sign and date the form, and mail it to:IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.
Form AR-11 Edition 11/02/22 Page 2 of 2
DHS Privacy Notice
AUTHORITIES: The information requested on this form is collected under the Immigration and Nationality Act (INA) section 265.
PURPOSE: The primary purpose for providing the requested information on this form is to report a change of address. Except for
those exempted, all aliens in the U.S. are required to report any change of address or new address. DHS uses the information you
provide to contact you about the immigration benefit you are seeking.
DISCLOSURE: The information you provide is mandatory. Failure to report a change of address may result in a fine, imprisonment
and/or removal (8 U.S.C. sections 1227(a)(3) and1306). Failure to comply could also jeopardize your ability to obtain a future visa or
other immigration benefits.
ROUTINE USES: DHS may share the information you provide on this form with other Federal, state, local, and foreign government
agencies and authorized organizations. DHS follows approved routine uses described in the associated published system of records
notices [DHS/USCIS-001 - Alien File, Index, and National File Tracking System and DHS/USCIS-007 - Benefits Information
System] and the published privacy impact assessments [DHS/USCIS/PIA-018 Alien Change of Address Card (AR-11)] which you can
find at www.dhs.gov/privacy. DHS may also share this information, as appropriate, for law enforcement purposes or in the interest
of national security.
All aliens subject to registration requirements may use this form to report a change of address within 10 days of such change. For
detailed instructions on how to update your address, please visit www.uscis.gov/addresschange. The collection of this information is
required by Immigration and Nationality Act (INA) section 265 (8 U.S.C. 1305). U.S. Citizenship and Immigration Services (USCIS)
uses the data collected on this form for statistical and record-keeping purposes, and may share this information with other Federal,
state, local, and law enforcement officials. Failure to report a change of address is punishable by fine or imprisonment and/or removal
from the United States.
NOTE: This form is not evidence of identity, age, or status claimed.
Address Change Information and Instructions
U.S. Department of Homeland Security
Citizenship and Immigration Services
Attn: Change of Address
1344 Pleasants Drive
Harrisonburg, VA 22801
Instructions
Complete all fields on this form, sign and date the form, and mail it to:
IMPORTANT: If you are in immigration proceedings, you must separately notify the Immigration Court of any address
changes. Filing Form AR-11 with USCIS does not update your address with the Immigration Court.

You should know that this text comes in a two-page PDF file. The text on the first page has been translated correctly, but the second page sends me the error. So what do I do?

I look forward to your help. Many thanks for the help.

richardsonoge commented 10 months ago

Can you paste your code fragment here? And possibly the content of PDF file also, via Pastebin or something like that (if the file is not private or sensitive)

Can you help me please?