public static function Decode($json, $toAssoc = false) { //Remove UTF-8 BOM if present, json_decode() does not like it. if (substr($json, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { $json = substr($json, 3); } try { $result = json_decode(trim($json), $toAssoc); $state = json_last_error(); if ($state !== JSON_ERROR_NONE) { $error = JSON::getErrorMessage($state); throw new JsonDecodeException("JSON Error ({$state}): {$error}"); } } catch (JsonDecodeException $e) { $line = trim(date("[d/m @ H:i:s]") . $e->getMessage()) . "\n"; if (preg_match('/<html[^>]{0,}>/i', $json)) { $message = preg_match('/<title>(.*)<\\/title>/i', $json); $line = trim("Error: " . $message[1]) . "\n"; } else { $line = trim("Debug:\n-->\n" . $json) . "\n<--\n"; } error_log($line, 3, LOG_FILE); return false; } return $result; }