'error', 'code' => 500, 'message' => 'Invalid url. The passed URL ' . $url . ' could not be loaded. ', 'reasons' => array('The page is not formatted with valid XML', 'The page is not available', 'The URL is invalid') ); try { $xml = simplexml_load_file($url); $jsonFromXML = json_encode($xml); // if json_encode fails, it - for some reason - returns 'false' as a String. if ($jsonFromXML != 'false') { echo $jsonFromXML; } else { echo json_encode($failureObj); } } catch (Exception $e) { json_encode($failureObj); exit(); }