Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Php and WordPress.

In Creation of a Flash arcade site using WordPress – step 4, we saw how to post a game into a wp database, now we’ll see how to retrieve game information.

It’s time to parse the json feed.

Where can I find the feed?
At this link http://www.mochiads.com/feeds/games/xxx/all/all?format=json you will find the json feed. Just replace the xxx with your publisher id.

Or use http://www.mochiads.com/feeds/games?format=json like I am doing in this example.

There are various solution according to your php settings. If you don’t know how to check your php settings, refer to phpinfo() at this link.

Php version 5.2.0 or above

If your server runs php 5.2.0 or above, you’re really lucky because it provides native json support.

In order to have the $mochi array as shown at lines 37-50 in Creation of a Flash arcade site using WordPress – step 4, you just need to use this script:

 $varvalue) {
		$mochi[$varname] = $varvalue;
	}
	// post game in wp
}

?>

and you’ll have everything you need in $mochi array. Then you’ll only have to include your posting routine at line 11

Php version before 5.2.0 or unknown php version

If your php version is older than 5.2.0 or you just’ don’t know what php version you are using, A Beautiful Site provides a class that works in the same way as 5.2.0 built-in functions.

Moreover, the class degrades if (or once) your server supports 5.2.0

You can download the class here and once uploaded JSON.php in the same directory of your parsing script, you just need to add

require("JSON.php");

as the first line of the parsing script.

Running out of memory

In some servers, you may run out of memory or getting strange errors (due to memory) while executing the script. You can save memory rewriting reduce_string function in the JSON.php file to

function reduce_string($str)
{  
   return trim($str);
}

since mochi feed does not have comments.

Running out of memory (again!!) or 500 Internal Server Error

If you get this message, then you should consider changing your provider.

Anyway, I made an awful script to make it work even on the cheapest server… but consider it’s not the best solution.

It works because it knows how mochi feed is made, so don’t try it with another json feed

 $varvalue) {
          $mochi[$varname] = $varvalue;
     }
     // post game in wp
     unset ($games[$x]);
}

?>

If you still experience errors, then you must change your hosting plan.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.