Add Game Ping

This URL will be pinged with the game ID sent via HTTP POST. This says that the game with that ID has been added to your site. If you use games.get then you can load the game information when dealing with this request. The HTTP User Agent used in this request would be your site's consumer secret key so you can use this to verify the request. You can use the post add game page to transfer the game details, images and even the SWF file.

Please note that when this ping URL is used, it is added to a batch list. It could take a few minutes before it's position in the batch is reached, so the game won't be added to your site instantly.

Example Code

The implementation of this is very straightforward. Example code of this page in PHP is as follows:

<?php
$gamecats
=array(13=>'action', 14=>'adventure', 15=>'arcade', 16=>'driving', 17=>'fighting', 18=>'sports', 19=>'shooting', 20=>'war'); //map each NewHaze category ID onto a category on your site
if(isset($_POST['gid'])) { //if a game ID has been posted
    
$dbc = @mysql_connect ('localhost', 'username', 'password'); //connect to the database
    
@mysql_select_db ('mydb'); //select the correct db
    
require_once('nh-api.php'); //include the NewHaze API code
    
$newhaze=new NewHaze('consumer_key','consumer_secret'); //enter consumer key and secret
    
$response=$newhaze->api->games_get(array($_POST['gid'])); //get the game with the same ID as posted
    
foreach($response->games as $game) { //go through all the results, getting $game from each one
        
$in_db=mysql_num_rows(mysql_query("SELECT * FROM games WHERE nh_id=".$game->id)); //check to see if it is already in the database
        
if($in_db==0) { //if not, add it
            
$category=$gamecats[$game->category->id]; //get the category identifier for your site
            
mysql_query("INSERT INTO games (nh_id, name, slug, description, category, width, height, swf_url, image1, image2) VALUES (".$game->id.", '".mysql_real_escape_string($game->name)."', '".mysql_real_escape_string($game->slug)."', '".mysql_real_escape_string($game->description)."', ".$category.", ".$game->flash->width.", ".$game->flash->height.", '".mysql_real_escape_string($game->flash->url)."', 'http://connect.newhaze.com/images/?gid=".$game->id."&width=150&height=100', 'http://connect.newhaze.com/images/?gid=".$game->id."&width=50&height=50')");
        }
    }
}
?>
Please note that there are some limitations to the code above. It does not save the game file or game images. You can easily add this in using the PHP copy function.


© 2012 NewHaze   Icons by Pixel Mixer   Help