Convert XML to JSON with PHP4

xml2json.pngIf you have an XML data source, but would like that data in JSON format, this is an alternative to fetching the XML and then converting it.

Instead, have a server fetch the data in XML format and convert it, so all you need to do is query that server.

Here is a working example.

I didn’t really write any of this myself. It’s just some parts I found on the internet, assembled so as to do this.

My implementation is quite specific to one XML data source - Dungeon Runners character data - but you will see how simply this might be modified to deal with any XML source.

One:

  • I use Snoopy to fetch my data. It’s a PHP file you’ll need to include, if you want to do the same. Rename that from class-snoopy.php.txt to merely class-snoopy.php.

Two:

  • I use JSON.php to convert to JSON format. Rename that from json.php.txt to merely json.php.

Three:

  • I include xml2array.php which I copied from this thread at PHP.net. I think if I made any modifications to it at all, they were trivial. Rename it from xml2array.php.txt to just xml2array.php.

Alright, now you must be wondering if I did anything myself at all. Well, yes I did.

I wrote index.php.

Now if you’re wanting to convert something other than the DR character data, you’ll need to modify that file.

I will explain what it is doing, so you will know what to change:

Include the stuff programmers wrote for me:

include('class-snoopy.php');
include('json.php');
include('xml2array.php');

Look for the character name in the URL and send the user to a very clever error/instruction page if it is not there.:

$char_name = $_GET['name'];
if (!$char_name) {
	header('Location: whoru.html');
	exit;
}

Otherwise, build the URL for our XML character data, and fetch it.

$url = 'http://www.dungeonrunners.com/characters/' . $char_name;
$charFile = fetch_remote_file ($url);

This is how I tell if we were returned an error message.

$pos = strpos($charFile, 'character not found');
if ($pos === false) {

If $pos is false then no part of the response was ‘character not found’, so parse it into an array.

	$objXML = new xml2Array();
	$arrOutput = $objXML->parse($charFile);

Comvert the array into JSON.

	$json = new Services_JSON();
	$output = $json->encode($arrOutput);

Print it.

	print $output;
} else {

Ok, this is where we are if ‘character not found’ was in the response. I send the user to a very clever error page.

	header('Location: whobdat.html');
	exit;
}

Finally, there’s a little more to “fetch that file”, but not much more. Here’s a function I wrote by looking at how WordPress played with Snoopy, and making the horrific assumption that since I was fetching the XML on behalf of some other user agent, that I should just keep that as the user agent.

function fetch_remote_file ($url) {
	$client = new Snoopy();
	$client->agent = $_SERVER['HTTP_USER_AGENT'];
	$client->read_timeout = 2;
	$client->use_gzip = true;
	@$client->fetch($url);
	return $client->results;
}

That’s it.

Please don’t be shy to point-out anything real stupid about this in the comments. Mind that I already know it’s a terrible alternative to using PHP5’s native functions… so that this solution ought to be stamped with a warning-label.

warning.png

And, done.

Leave a comment expressing your frustration with how much of your time that post just wasted:

Your email is never published nor shared. Required fields are marked *

*
*