OK so I have seen that I am getting more and more hits on my site for “json to xml”+”php”.

This is all fine and well, as I know that the last major Gamertag Information API was made by Duncan and was in XML.
So what I am giving to you all today is a PHP script to convert JSON (fast and low footprint) to XML (not so) witin PHP.

Simple to use, and hopefully understand.

PHP Code:

< ?php

function ARRAYtoXML($array, $depth = 0){
    $indent = '';
    $return = '';
    for($i = 0; $i < $depth; $i++)
        $indent .= "\t";
    foreach($array as $key => $item){
        $return .= "{$indent}< {$key}>\n";
        if(is_array($item))
            $return .= ARRAYtoXML($item, $depth + 1);
        else
            $return .= "{$indent}\t< ![CDATA[{$item}]]>\n";
            $return .= "{$indent}\n";
        }
    return $return;
}

$json_data = file_get_contents('http://djekldev.co.uk/card/json.php?tag=djekl');
$data = json_decode($json_data, true);

@header("Content-Type: text/xml");
print '< ?xml version="1.0" ?>' . "\n";
print ARRAYtoXML($data);

?>

CURL Function replacement for ‘file_get_contents()’:

	// This is a CURL replacement to 'file_get_contents()'.
	function get_contents($url)
	{
		$url = str_replace(" ", "%20", $url);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt ($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($ch);
		curl_close($ch);

		return $output;
	}

Example Output:


	
		< ![CDATA[true]]>
	
	
		< ![CDATA[djekl]]>
	
	
		

			< ![CDATA[http://avatar.xboxlive.com/avatar/djekl/avatarpic-l.png]]>
		
		
			< ![CDATA[http://avatar.xboxlive.com/avatar/djekl/avatarpic-s.png]]>
		
		
			< ![CDATA[http://avatar.xboxlive.com/avatar/djekl/avatar-body.png]]>
		
	

	
		< ![CDATA[Gold]]>
	
	
		< ![CDATA[53346]]>
	
	
		< ![CDATA[500]]>
	

	
		< ![CDATA[Male]]>
	
	
		< ![CDATA[false]]>
	
	
		< ![CDATA[Newcastle, England]]>
	

	
		< ![CDATA[www.twitter.com/djekl]]>
	
	
		< ![CDATA[Loading...™]]>
	
	
		< ![CDATA[Quitting the game since 1988...

Fallen friends will never be forgotten!
Lets hope I'm not when its my time.]]>
	

	
		
			< ![CDATA[false]]>
		
		
			< ![CDATA[false]]>
		
		
			< ![CDATA[false]]>

		
	
	
		
			
				< ![CDATA[Skyrim]]>
			
			
				< ![CDATA[425307E6]]>

			
			
				< ![CDATA[http://marketplace.xbox.com/Title/1112737766]]>
			
			
				< ![CDATA[http://live.xbox.com/en-US/GameCenter/Achievements?titleId=1112737766&compareTo=djekl]]>
			
			
				< ![CDATA[http://tiles.xbox.com/tiles/qR/QI/1Gdsb2JhbA9ECgQKGgMfWSpVL2ljb24vMC84MDAwIAAAAAAAAPsnFLY=.jpg]]>

			
			
				< ![CDATA[11/15/2011]]>
			
			
				< ![CDATA[115]]>
			
			
				< ![CDATA[1000]]>

			
			
				< ![CDATA[11]]>
			
			
				< ![CDATA[50]]>
			
			
				< ![CDATA[22%]]>

			
			
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/425307E6/en-US/smallboxart.jpg]]>
				
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/425307E6/en-US/largeboxart.jpg]]>
				
			

		
		
			
				< ![CDATA[Modern Warfare® 3]]>
			
			
				< ![CDATA[415608CB]]>
			
			

				< ![CDATA[http://marketplace.xbox.com/Title/1096157387]]>
			
			
				< ![CDATA[http://live.xbox.com/en-US/GameCenter/Achievements?titleId=1096157387&compareTo=djekl]]>
			
			
				< ![CDATA[http://tiles.xbox.com/tiles/63/-y/0mdsb2JhbA9ECgQJGgYfViwhL2ljb24vMC84MDAwIAAAAAAAAP3df-Q=.jpg]]>
			
			

				< ![CDATA[11/9/2011]]>
			
			
				< ![CDATA[65]]>
			
			
				< ![CDATA[1000]]>
			
			

				< ![CDATA[7]]>
			
			
				< ![CDATA[50]]>
			
			
				< ![CDATA[14%]]>
			
			

				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/415608CB/en-US/smallboxart.jpg]]>
				
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/415608CB/en-US/largeboxart.jpg]]>
				
			
		
		

			
				< ![CDATA[Battlefield 3]]>
			
			
				< ![CDATA[45410950]]>
			
			
				< ![CDATA[http://marketplace.xbox.com/Title/1161890128]]>
			

			
				< ![CDATA[http://live.xbox.com/en-US/GameCenter/Achievements?titleId=1161890128&compareTo=djekl]]>
			
			
				< ![CDATA[http://tiles.xbox.com/tiles/Fb/Ys/02dsb2JhbA9ECgQNGwEfV1pTL2ljb24vMC84MDAwIAAAAAAAAPwDtgo=.jpg]]>
			
			
				< ![CDATA[11/8/2011]]>
			

			
				< ![CDATA[300]]>
			
			
				< ![CDATA[1000]]>
			
			
				< ![CDATA[14]]>
			

			
				< ![CDATA[38]]>
			
			
				< ![CDATA[36%]]>
			
			
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/45410950/en-US/smallboxart.jpg]]>

				
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/45410950/en-US/largeboxart.jpg]]>
				
			
		
		
			
				< ![CDATA[Call of Duty ELITE]]>

			
			
				< ![CDATA[41560898]]>
			
			
				< ![CDATA[http://marketplace.xbox.com/Title/1096157336]]>
			
			
				< ![CDATA[http://live.xbox.com/en-US/GameCenter/Achievements?titleId=1096157336&compareTo=djekl]]>

			
			
				< ![CDATA[http://tiles.xbox.com/tiles/S9/J4/0mdsb2JhbA9ECgQJGgYfVlZbL2ljb24vMC84MDAwIAAAAAAAAP1X0lQ=.jpg]]>
			
			
				< ![CDATA[11/8/2011]]>
			
			
				< ![CDATA[0]]>

			
			
				< ![CDATA[0]]>
			
			
				< ![CDATA[0]]>
			
			
				< ![CDATA[0]]>

			
			
				< ![CDATA[0%]]>
			
			
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/41560898/en-US/smallboxart.jpg]]>
				
				

					< ![CDATA[http://tiles.xbox.com/consoleAssets/41560898/en-US/largeboxart.jpg]]>
				
			
		
		
			
				< ![CDATA[Forza Motorsport 4]]>
			
			

				< ![CDATA[4D530910]]>
			
			
				< ![CDATA[http://marketplace.xbox.com/Title/1297287440]]>
			
			
				< ![CDATA[http://live.xbox.com/en-US/GameCenter/Achievements?titleId=1297287440&compareTo=djekl]]>
			
			

				< ![CDATA[http://tiles.xbox.com/tiles/9R/uF/1Gdsb2JhbA9ECgR8GgMfV15TL2ljb24vMC84MDAwIAAAAAAAAPuqG+o=.jpg]]>
			
			
				< ![CDATA[11/1/2011]]>
			
			
				< ![CDATA[405]]>
			
			

				< ![CDATA[1000]]>
			
			
				< ![CDATA[30]]>
			
			
				< ![CDATA[48]]>
			
			

				< ![CDATA[62%]]>
			
			
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/4D530910/en-US/smallboxart.jpg]]>
				
				
					< ![CDATA[http://tiles.xbox.com/consoleAssets/4D530910/en-US/largeboxart.jpg]]>