Creation of a Flash arcade site using WordPress – step 5

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
 
$mochi_url = "http://www.mochiads.com/feeds/games?format=json"; 
$feed = file_get_contents($mochi_url); 
$games_array = json_decode($feed, true);
 
$number_of_games = count($games_array[games]);
 
for($x=0;$x<$number_of_games;$x++){
	foreach($games_array[games][$x] as $varname => $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

1
2
3
4
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
 
require("json.php");
 
$mochi_url = "http://www.mochiads.com/feeds/games?format=json";
$feed = file_get_contents($mochi_url);
 
$feed_start=strpos($feed,"[{");
 
$feed = substr($feed,$feed_start+1);
$feed = substr($feed,0,strlen($feed)-2);
$games = explode("{\"rating\": \"",$feed);
unset($feed);
for($x=1;$x<=count($games);$x++){
     $games[$x] = "{\"rating\": \"".$games[$x];
     $games[$x] = substr($games[$x],0,strlen($games[$x])-2);
     $games_decode = json_decode($games[$x], true);
     foreach($games_decode as $varname => $varvalue) {
          $mochi[$varname] = $varvalue;
     }
     // post game in wp
     unset ($games[$x]);
}
 
?>

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

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 3.83 out of 5)
Loading ... Loading ...
WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 16 comments

  1. Mark Loika

    on August 24, 2008 at 10:16 pm

    Hey Emanuele! Your blog has inspired me to do so many things! I’ve made games and gotten them sponsored, and now I’ve made a blog! Check it out:

    http://flashdevz.wordpress.com/

  2. Erik

    on August 25, 2008 at 7:37 pm

    but won’t this script add games that exist or is it still not in the add part?

  3. Games Freez

    on August 26, 2008 at 3:54 am

    Emanuele, thanks, you rocks!

    Erik, as long as the game tag is in the database, it will not add the game again.

    However if a new version of same game using the same game tag, it will not update either. Deleting the old “post” might work.

  4. Bob

    on August 30, 2008 at 2:11 am

    Thanks for new step.
    But could you please offer us a complete version of this script? Oh I’m not lazy, But don’t know much about programming.
    Thanks.

  5. Killy

    on August 30, 2008 at 9:53 pm

    So where do you put that code and how do you make it work? I’ve got everything working fine so far even the adding of the one game to the wordpress database and displaying it. But where does this code go? I’m quite confused as you may have worked out.

    Hope you can tell me how to get it all to work.

    Killy

  6. Emanuele Feronato

    on August 30, 2008 at 11:31 pm

    I am preparing the next step…

  7. Killy

    on August 31, 2008 at 11:25 am

    oh, ok cheers for that :) look forward to it

  8. Bob

    on September 3, 2008 at 9:52 pm

    Woohooooo thank you Emanuele.
    You’re so kind.
    Could you learn us how can i manage games with that script in next step?
    I mean change that script in a way so that for example we can see last 10 games and check what games we want to add.
    Thanks.

  9. Snoozy

    on September 16, 2008 at 11:21 pm

    Thanks for your tutorial. Just one comment/suggestion… in my case, Mochiads Json is so long that I had to add a “set_time_limit(xx);” in order to be able to execute all the script. Otherwise the standard 30 sec. isn’t enough long.

    Thanks again. Continue like this !!
    Alain
    playZgame.com

  10. mrfixit

    on November 8, 2008 at 4:17 am

    can you please explain where this code is supposed to go?

  11. mrfixit

    on November 8, 2008 at 4:50 am

    i put it where i thought it was supposed to go and i get Fatal error: Maximum execution time of 90 seconds exceeded in /home/&&/hidden/hidden/public_html/&&/JSON.php on line 701
    anyway you could set this up on my system or help me get it going?

  12. cryptickilla4f

    on November 19, 2008 at 9:55 pm

    Can someone plllease show me a working version of this script. I need to try the non php 5 version on my current server to find out if i need a new host or not. If anyone can recommend an affordable host that can handle this script please do.
    Thanks

  13. rbostan

    on January 8, 2009 at 8:33 pm

    hi, when i try to run script, it is waiting for a time, then it says
    Duplicate entry ’40-1′ for key 1

    40-1 changing every try

    idea ?

    thanks

  14. emanuel mocean

    on January 30, 2009 at 5:34 pm

    Hi.
    First a thousand thank you .
    I manage to make it work , but it only loads about 10 games at a time so I have to run the script till I go crazy :) .Could the free host be the problem ?
    Also isn’t it dangerous to just let scripts for everyone to run ?
    Thanks

  15. Alex

    on April 4, 2009 at 6:40 am

    Hello,
    I have followed this series of tutorials and have got to the point of automating the Mochiads feed. I am not sure exactly where to put the code and I have tried numerous places. The best I could get was a broken feed where it would say “record not inserted” about 50 times while maybe saying “record inserted” once in the whole thing and then ending with an error speaking of a duplicate or of some weird thing related to the descriptions of the games. I would really appreciate some help on how to make them all say “record inserted” so that I can have the games on my arcade.
    Thanks

  16. Hacer un sitio con Arcades sobre WordPress | Summarg

    on May 23, 2009 at 2:37 am

    [...] Creation of a Flash arcade site using WordPress – Step 5: Ahora si, se agregan juegos desde el feed y habla sobre algunos problemas frecuentes y sus soluciones. [...]