I am pulling data from a 3rd party web service and when the api goes under maintenance the json document no longer has the same structure (but is still a valid json document). So when I go to parse, I get an error: "Invalid argument supplied for foreach()" Makes sense, but how do I test for this and then call a separate function to log the error with the contents of the json document?
I tried adding set_error_handler("customError"); and that works but I don't know how to access the contents of $response or $json from the error function
$response = file_get_contents($url);
$json = json_decode($response, TRUE);
foreach($json['workers'] as $item) {
echo $item['address']; //address does not exist when in maintenance mode!
}
function customError($errno, $errstr) {
$link=Connection();
echo "<b>Error:</b> [$errno] $errstr ";
$sql="Insert into myErrors (response, description) values ('".$response."','.$errstr.');";
//echo $sql;
$result = mysqli_query( $link,$sql) or die('Error; ' . mysqli_error($link));
}