PHP: Is SimpleXML Loaded?
There are a couple plugins that I’ve built that require PHP5+ and SimpleXML. SimpleXML is a much easier and better performing method of parsing XML responses from APIs. The problem, though, is that I would get at a few emails a day or week asking me why the user couldn’t load the program and it resulted in errors.
Apparently, my notices on the plugins and on the project pages weren’t enough, so I did the right thing and added functionality to both plugins to verify the SimpleXML extension is loaded.
PHP Function to check the SimpleXML extension is loaded:
function isSimpleXMLLoaded() { $array = array(); $array = get_loaded_extensions(); $result = false; foreach ($array as $i => $value) { if (strtolower($value) == "simplexml") { $result = true; } } return $result; }
Now, within the functions that use SimpleXML, I can simply ensure it’s loaded before I actually try the SimpleXML call. If
if (!isSimpleXMLLoaded()) { echo "Host your site somewhere else!"; return; }
I know I’ve got some PHP gurus that keep an eye on my blog, let me know how I did! I’ve released minor updates to both Plugins to utilize this method.