statickidz / php-google-translate-free

PHP class to use the Google Translator API for free.
GNU General Public License v3.0
271 stars 91 forks source link

Invalid argument supplied for foreach() #16

Closed gsahu125 closed 3 years ago

gsahu125 commented 6 years ago

I am getting this error, please help me how to fix it (1/2) ErrorExceptionInvalid argument supplied for foreach()

in GoogleTranslate.php (line 123)

i am using it like this & previously it was working fine <?php use \Statickidz\GoogleTranslate; //start translater config $source = 'es'; $target = 'en'; $text = 'verdadero';

$trans = new GoogleTranslate(); $result = $trans->translate($source, $target, $text);

echo $result; ?>

mobinzk commented 6 years ago

Following to this issue, it seems Google has blocked our websites/IPs. Same thing happen to me, here is the full result we get back after the curl request.


<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.com/sorry/index?continue=https://translate.google.com/translate_a/single%3Fclient%3Dat%26dt%3Dt%26dt%3Dld%26dt%3Dqca%26dt%3Drm%26dt%3Dbd%26dj%3D1%26hl%3Des-ES%26ie%3DUTF-8%26oe%3DUTF-8%26inputm%3D2%26otf%3D2%26iid%3D1dd3b944-fa62-4b55-b330-74909a99969e&amp;hl=es-ES&amp;q=EgQ0EQ-rGIKEqdkFIhkA8aeDS9DN_fLOLB0QMdCMDhKDj1xB5-BuMgFy">here</A>.
</BODY></HTML>```
mobinzk commented 6 years ago

I've been working on a solution, unfortunately there is no anyway go around this limitation other than changing the server IP address or using the payed API. In addition, if you don't send any request from that IP address the IP gets unblocked after 2 to 48 hours.

I believe this is not an issue with this awesome wrapper and we can close this issue.

afgaiel commented 6 years ago

any workaround? can we change that IP to dynamic IP or something like localhost? any other translator using https://translate.google.com/ from localhost?

Thanks for your great efforts and hope to get solved very soon.

kishan1812 commented 5 years ago

I was use this code in REST API for convert language english to gujarati but give error and response is blank please help to solve this issue. Severity: Warning Message: Invalid argument supplied for foreach() Filename: src/GoogleTranslate.php Line Number: 123

public function organization_post() { $OrgName = $this->input->get_post("OrgName", ""); $OrgCity = $this->input->get_post("OrgCity", ""); $perpage = $this->input->get_post("perpage", ""); $offset = $this->input->get_post("offset", ""); $lang = $this->input->get_post("lang", "");

    $orgquery = "SELECT OrgId, OrgName, OrgCity, OrgContact, OrgDetails, OrgAddress, OrgImage FROM organization WHERE OrgStatus='1' and OrgApprovedStatus='1' and OrgIsAlive='1' ";

    if($OrgName)
    {
        $orgquery.="AND OrgName like '%".$OrgName."%' "; 
    }
    if($OrgCity)
    {
        $orgquery.="AND OrgCity like '%".$OrgCity."%'"; 
    }
    $orgquery.="ORDER BY OrgId DESC";

    if ($perpage != '' && $offset != '') {
        $orgquery .= " LIMIT " . $offset . "," . $perpage . " ";
    }

    $organization = $this->MainModal->getResultRows($orgquery);
    $count = count($organization);

    if ($count > 0) {
        for($i = 0; $i < $count; $i++){

            $id = $organization[$i]['OrgId'];

            $orgmem = "SELECT OrgmId, OrgPersonName, OrgPersonContact, OrgPersonPosition FROM organization_member WHERE OrgId = $id ";
            $orgmemdata = $this->MainModal->getResultRows($orgmem);

            if($lang == 1){
                if(isset($organization[$i])){
                    if(!empty($organization[$i])){
                        foreach($organization[$i] as $oky => $ovalue){
                            if($oky != 'OrgImage'){
                                $dataarray[$oky] = $this->gujarati($ovalue);
                            }else{
                                $dataarray[$oky] = $ovalue;
                            }
                        }
                    }
                }
                if(isset($orgmemdata)){
                    if(!empty($orgmemdata)){
                        foreach($orgmemdata as $key => $value){
                            foreach($value as $ky => $mam_values){
                                $mem_array[$ky] = $this->gujarati($mam_values);
                            }
                            $me_dataarray[] = $mem_array;
                        }
                        $dataarray['member'] = $me_dataarray;
                    }
                }
                $final_array[$i] = $dataarray;
                unset($me_dataarray);
            }
            else{
                $dataarray = $organization[$i];
                $dataarray['member'] = $orgmemdata;
                $final_array[$i] = $dataarray;
            }
        }
        $return_array["status"] = $this->true;
        $return_array["message"] = "Organization found successfully";
        $return_array['result'] = $final_array;             

    } else {
        $return_array["status"] = $this->false;
        $return_array["message"] = "Organization Not found";
        $this->response(['data' => $return_array], 200, true);
    }
    $this->response(['data' => $return_array], 200, true);
}
afgaiel commented 5 years ago

for the blank response, you need to add some delay for each request which means if you have a file and you read this file line by line you need to insert some delay between reading each line. hope it helps you.

jotabastosjoin commented 5 years ago

Same story, it still does not work!

beppe9000 commented 5 years ago

yeah me too... both invalid argument and empty translation

renziito commented 5 years ago

Hey, its google fault not this wrapper .. Google says "We detect a unsual activities from your red" or something like that will produce an error...

beppe9000 commented 5 years ago

Right now I am using this and yandex for translations it has a free tier with 8m characters/month of translations and works great for when google blocks me (i'm not even logging that anymore...).

Maybe some error handling and optional fallback to input string could help mitigate the scenario for other people?

Another idea could be to include a default minimum sleep time between the actual calls that can be overridden by parameter, constructor or property.

renziito commented 5 years ago

i made a pull request with an exception for now.. but your ideas are way better.. on error return same text (maybe?).

beppe9000 commented 5 years ago

Great!

BTW, with some default behaviors the signature can become more complete like this for example...

function translate($source, $target, $text, $fallback='', $sleep_seconds = 5, &$error=null)

If no error occurred $error should be set to false (so it's easy to check), else the catched exception can be assigned to it so that it can be used for further processing or logging by the user code.

If $fallback is null then $text should be returned in case of error, otherwise returns $fallback. Default should be an empty string for backward compatibility... or null for a new major version IMHO.

The $sleep_seconds is pretty self-explanatory: should allow the user to specify how many seconds between each request. Also I was thinking if $sleep_seconds is negative we could use that as a switch to use a temp memory-mapped file with a well known filename to keep track of time globally (different php files that include it/separate urls/users/page refresh/etc).

Just my $0.02 ideas :)

Ps. This library could probably use some optional form of cookie support... it may actually decrease freeloader detection errors... :)

renziito commented 5 years ago

Hey @beppe9000 maybe you can help @statickidz and do it? :stuck_out_tongue:

statickidz commented 5 years ago

I’m currently out of time by work but if you send some pull request I can check and merge it

renziito commented 5 years ago

You hear (read) the man @beppe9000 make it happen :+1:

beppe9000 commented 5 years ago

I’m currently out of time by work but if you send some pull request I can check and merge it

Me too that and exams, but will certainly do that once pressure decreases

kaliloulah1155 commented 4 years ago

Somebody find a solution please ?