jbradley04 Posted November 18, 2012 Share Posted November 18, 2012 So I am running a script in a crontab and I keep getting an error. The File still executes but it errors out in my log. The error says: PHP Warning: Invalid argument supplied for foreach() in /var/www/clients/client1/web/for/PF.php on line 20 Basically, what I am doing is taking a DB of Addresses that I have and confirming that the zipcodes are correct, if not I am updating them according to Google. The code I have is this: ///// CHECK ADDRESSES FROM GOOGLE /////// $sql = "SELECT * FROM $table2 WHERE `checked` <> 2 LIMIT $limit"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)) { $id = $row['id']; $street = $row['St']; $city = $row['City']; $state = $row['State']; $oldzip = $row['Zip']; $address=urlencode($street.",".$city.",".$state); $gsend = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=".$address."&sensor=true"); $json = json_decode($gsend); $postal = FALSE; foreach( $json->results[0]->address_components as $component ) { if ( in_array('postal_code', $component->types) ) $postal = $component->long_name; if ( $postal ) { if ($oldzip != $postal) { mysql_query("UPDATE $table2 SET `updated` = 2, `Zip_Code`='$postal' WHERE `id` = '$id'"); } else { mysql_query("UPDATE $table2 SET `updated` = 1 WHERE `id` = '$id'"); } } } mysql_query("UPDATE $table2 SET `checked` = 2 WHERE `id` = '$id'"); } Line 20 is the foreach command line. Any help would be greatly appreciated! Thanks J Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 18, 2012 Share Posted November 18, 2012 Obviously $json->results[0]->address_components is not an array. I can think of a few problems to look at: 1) $result does not contain the values you think it does (i.e. 'id', 'St', 'City', etc.) 2) $address is not formatted the way it needs to be 3) file_get_contents() is failing. Check that the address you are specifying is correct and that file_get_contents() can retrieve those contents 4) The results of json_decode($gsend) does not contain the array you think at $json->results[0]->address_components Check all of the above and I'm sure you'll find the error Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.