Poker Mavens - API Examples (Legacy)

Updated 2014-05-30

Introduction

Note: this page includes API Examples that work with versions of Poker Mavens prior to 4.20, when JSON formatting was first included as an option. Visit API_Examples.htm to see the newer examples.

The Application Programming Interface (API) for Poker Mavens can be accessed by any programming language and from any computer that can make HTTP GET/POST calls to your server's file port. Most of the examples below use the free PHP web scripting language, universally available on most web hosting sites (Linux and Windows). There is also an example in classic ASP, which is a Windows-only scripting system included with the Windows IIS web server. You may download all of these files in zip format or copy and paste them individually from the sections below:

Security

Keep in mind that the API password is passed as one of the parameters on every call from the computer running the PHP script to the computer running the game server. Therefore you should only allow the scripts to be installed on a system that you have control over. ASP and PHP code is executed on the server side so that the source is not visible to the person running it from their browser. However, if the scripting processor somehow became disabled, the server could send the raw script back to the user. To prevent your API password from being exposed in this case, you can place it in a separate file and store it in a directory located above the www directory. Then it would only be accessible via the local file system and not via HTTP from the outside world.

For example, if the root of your web site is located at this path:

/home/username/www/

then you can create a sibling directory like this which will not be accessible via HTTP:

/home/username/passwords/

and then save a 1-line API_Password.php file there with this content, substituting in your own API password:

<?php $pw = "my_api_password"; ?>

Then use the PHP include command in your main program to set the $pw variable to your API password:

<?php
  include "/home/username/passwords/API_Password.php";
  ...
?>

ASP has a similar include command. You can create a virtual folder for IIS and store your password there. Then reference it with an include statement:

<!-- #include virtual="/includes/API_Password.asp" -->

Poker_API Function (PHP)

The following PHP file (API.php) will be included in the examples to follow for interfacing with the game server. The Poker_API function uses the free PHP libcurl extension to make a POST call to your game server, passing it the desired parameters, and then returns either a regular or an associative array with the results.

If you get an error message indicating that curl_init() is an undefined function, then you do not have the libcurl extension installed with your PHP setup. If your web site is on a managed system, contact your site operator for assistance. If you're installing PHP locally using EasyPHP, then all you have to do is right-click the EasyPHP icon in the sytem tray, select Configuration, PHP Extension, and then make sure php_curl is checked in the extension list.

Before saving the code below to a file called API.php, change the $url variable to your actual game server address (with file port and /api path). Change the $pw variable to your actual API password, or better yet, replace that whole line with an include command as described in the Security section above.

<?php

  $url = "http://192.168.1.100:8087/api";     // <-- use your game server IP
  $pw = "my_api_password";                    // <-- use your api password

  function Poker_API($url, $params, $assoc)
  {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $response = trim(curl_exec($curl));
    if (curl_errno($curl)) $cerror = curl_error($curl); else $cerror = "";
    curl_close($curl);
    $api = Array();
    if ($assoc)  // associative array result
    {
      if ($cerror != "")
      {
        $api["Result"] = "Error";
        $api["Error"] = $cerror;
      }
      else if (empty($response))
      {
        $api["Result"] = "Error";
        $api["Error"] = "Connection failed";
      }
      else  
      {
        $paramlist = Explode("\r\n", $response);
        foreach ($paramlist as $param)
        {
          $namevalue = Explode("=", $param, 2);
          $api[$namevalue[0]] = $namevalue[1];
        }
      }
    }
    else  // regular array result
    {
      if ($cerror != "")
      {
        $api[] = "Result=Error"; 
        $api[] = "Error=" . $cerror; 
      }
      else if (empty($response))
      {
        $api[] = "Result=Error"; 
        $api[] = "Error=Connection failed"; 
      }
      else  
      {
        $paramlist = Explode("\r\n", $response);
        foreach ($paramlist as $param) $api[] = $param;
      }
    }
    return $api;
  }

?>

Poker_API Function (ASP)

The following ASP file (API.asp) is the classic ASP equivalent of the Poker_API function described above. Classic ASP (as opposed to the newer ASP.Net system) is not enabled by default on newer implementations of IIS. Instructions for enabling classic ASP are available here.

<%
  url = "http://127.0.0.1:8087/api"   ' replace with your poker server IP
  pw = "my_api_password"              ' replace with your API password

  function Poker_API(url, params, assoc)
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
    on error resume next
    xmlhttp.open "POST", url, false 
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    xmlhttp.send params
    rt = xmlhttp.responseText 
    set xmlhttp = nothing
    if assoc = true then
      set api = CreateObject("Scripting.Dictionary")
      if rt = "" then
        api.add "Result", "Error"
        api.add "Error", "Connection failed"
      else
        result = split(rt, vbCrLf) 
        for each item in result
          keyvalue = split(item, "=")
          api.add keyvalue(0), keyvalue(1)
        next
      end if
      set Poker_API = api
    else
      if rt = "" then
        dim api(1)
        api(0) = "Result=Error"
        api(1) = "Error=Connection failed" 
      else
        api = split(rt, vbCrLf)
      end if
      Poker_API = api
    end if
  end function
%>

See the System Stats section below for an example of how to use this function.


System Stats (PHP)

This is a very simple example (SysStats.php) that calls the SystemStats command and displays the data returned, including the current number of logins, filled seats, occupied tables, and server up time.

<html>
<body>
<?php
  include "API.php";  // $pw and $url set in this file
  $params = "Password=" . $pw . "&Command=SystemStats";
  $api = Poker_API($url,$params,true);
  echo "<h3>Poker Server Status</h3>\r\n";
  if ($api["Result"] == "Error") die("Error: " . $api["Error"]);


  echo "Logins: " . $api["Logins"] . "<br/>\r\n";
  echo "Filled Seats: " . $api["FilledSeats"] . "<br/>\r\n";
  echo "Occupied Tables: " . $api["OccupiedTables"] . "<br/>\r\n";
  echo "Up Time: " . $api["UpTime"] . "<br/>\r\n";
?>
</body>
</html>

System Stats (ASP)

This file (SysStats.asp) is equivalent to the PHP version above.

<html>
<body>
<!-- #include file="API.asp" -->
<% 
  params = "Command=SystemStats&Password=" & pw
  set api = Poker_API(url, params, true)
  if api.item("Result") = "Error" then
    response.write("Error: " & api.item("Error"))
  else
    response.write("Logins: " & api.item("Logins") & "<br />" & vbCrLf)
    response.write("Filled Seats: " & api.item("FilledSeats") & "<br />" & vbCrLf)
    response.write("Occupied Tables: " & api.item("OccupiedTables") & "<br />" & vbCrLf)
    response.write("Up Time: " & api.item("UpTime") & "<br />")
  end if
%>
</body>
</html>

Chip Leaders

This example (ChipLeaders.php) displays the Top 10 chip leaders on your site. It requires the API.php file described above.

<html>
<body>
<?php

  $topcount = 10;  // display top 10, use any number you like

  include "API.php";  // API $url and $pw values set here

  // Fetch the Site Name using the SystemGet API command.

  $params = "Password=" . $pw . "&Command=SystemGet&Property=SiteName";
  $api = Poker_API($url,$params,true);
  $result = $api["Result"];
  if ($result == "Error") die("Error: " . $api["Error"]);
  $sitename = $api["Value"];

  // Fetch the list of players using the AccountsList API command.

  $params = "Password=" . $pw . "&Command=AccountsList&Fields=Player,Balance";
  $api = Poker_API($url,$params,true);
  $result = $api["Result"];
  if ($result == "Error") die("Error: " . $api["Error"]);

  // Iterate through the players in the response and create a associative
  // chips array keyed on player name.

  $accounts = $api["Accounts"];
  $chips = Array();
  for ($i = 1; $i <= $accounts; $i++)
  {
    $player = $api["Player" . $i];
    $chips[$player] = $api["Balance" . $i];
  }

  // Sort array in decending order.

  arsort($chips);

  // Display results in an html table.

  echo "<table border='1' cellpadding='5'>\r\n";
  echo "<tr><th colspan='3'>$sitename - Chip Leaders</th></tr>\r\n";
  echo "<tr><th>Rank</th><th>Player</th><th>Balance</th></tr>\r\n";
  $rank = 0;
  foreach ($chips as $p => $c)
  {
    $rank++;
    echo "<tr><td>$rank</td><td>$p</td><td>$c</td></tr>\r\n";
    if ($rank == $topcount) break;
  }
  echo "</table><br>\r\n";

?>
</body>
</html>

Create New Account

This is a PHP example for creating new accounts using a web site interface. It even graphically displays the list of available avatars (set the $avatarXXX variables to match your info):

The following example (NewAcct.php) works in version 2 and 3 where the avatars are in separate images:

<html>
<body>
  
  <?php

    $avatarurl = "http://192.168.1.100:8087/avatar";   // set your url here
    $avatarmax = 64;       // number of avatars available
    $avatarswf = false;    // set to true if custom avatars are in swf format

    include "API.php";

    if (isset($_REQUEST["Submit"]))
    {
      $Player = $_REQUEST["Player"];
      $RealName = $_REQUEST["RealName"];
      $Gender = $_REQUEST["Gender"];
      $Location = $_REQUEST["Location"];
      $Password1 = $_REQUEST["Password1"];
      $Password2 = $_REQUEST["Password2"];
      $Email = $_REQUEST["Email"];
      $Avatar = $_REQUEST["Avatar"];
      if ($Password1 <> $Password2) die("Password mismatch. Click Back Button to correct.");
      $params = "Password=$pw&Command=AccountsAdd" .
                "&Player=" .   urlencode($Player) .
                "&RealName=" . urlencode($RealName) .
                "&PW=" .       urlencode($Password1) .

                "&Location=" . urlencode($Location) .
                "&Email=" .    urlencode($Email) .
                "&Avatar=" .   urlencode($Avatar) .
                "&Gender=" .   urlencode($Gender) .
                "&Chat=" .     "Yes" .
                "&Note=" .     urlencode("Account created via API");
      $api = Poker_API($url,$params,true);
      if ($api["Result"] == "Ok") echo "Account successfully created for $Player";
      else echo "Error: " . $api["Error"] . "<br>Click Back Button to correct.";
      exit;
    }
  ?>

  <h3>Create New Account</h3>
  <form method="post">
    <table>
    <tr><td>Your player name:</td><td><input type="text" name="Player" /></td></tr>
    <tr><td>Your real name:</td><td><input type="text" name="RealName" /></td></tr>
    <tr><td>Your gender:</td><td><input type="radio" name="Gender" Value="Male" checked>Male</input> &nbsp; 
                 <input type="radio" name="Gender" Value="Female">Female</input></td></tr>
    <tr><td>Your location:</td><td><input type="text" name="Location" /></td></tr>
    <tr><td>Select a password:</td><td><input type="password" name="Password1" /></td></tr>
    <tr><td>Confirm password:</td><td><input type="password" name="Password2" /></td></tr>
    <tr><td>Your email address:</td><td><input type="text" name="Email" /></td></tr>
    <tr><td>Your avatar:</td><td>
    <div style="width: 100px; height: 175px; overflow: auto; border: solid 2px">
      <?php
        for ($i=1; $i<=$avatarmax; $i++)
        {
          $s = "<input type='radio' name='Avatar' value='$i'";
          if ($i == 1) $s .= " checked";
          $s .= ">"; 
          if ($avatarswf)
          {
            $s .= "<object width='48' height='48'>" .
                  "<param name='movie' value='$avatarurl?Index=$i'>" .
                  "<embed src='$avatarurl?Index=$i' width='48' height='48' type='application/x-shockwave-flash'>" .
                  "</embed>" .
                  "</object>";
          }
          else $s .= "<img src='$avatarurl?Index=$i' align='middle'>";
          echo $s . "<br>\r\n";
        }
      ?>
    </div>
    </td></tr>
    </table>
    <input type="submit" name="Submit" value="Submit" />
  </form>

</body>
</html>

This example (NewAcct4.php) works in version 4 where the avatars are all in one large image:

<html>
<body>
  
  <?php

    $avatarurl = "http://192.168.1.100:8087/Image?Name=Avatars";   // set your url here
    $avatarmax = 64;       // number of avatars available

    include "API.php";

    if (isset($_REQUEST["Submit"]))
    {
      $Player = $_REQUEST["Player"];
      $RealName = $_REQUEST["RealName"];
      $Gender = $_REQUEST["Gender"];
      $Location = $_REQUEST["Location"];
      $Password1 = $_REQUEST["Password1"];
      $Password2 = $_REQUEST["Password2"];
      $Email = $_REQUEST["Email"];
      $Avatar = $_REQUEST["Avatar"];
      if ($Password1 <> $Password2) die("Password mismatch. Click Back Button to correct.");
      $params = "Password=$pw&Command=AccountsAdd" .
                "&Player=" .   urlencode($Player) .
                "&RealName=" . urlencode($RealName) .
                "&PW=" .       urlencode($Password1) .
                "&Location=" . urlencode($Location) .
                "&Email=" .    urlencode($Email) .
                "&Avatar=" .   urlencode($Avatar) .
                "&Gender=" .   urlencode($Gender) .
                "&Chat=" .     "Yes" .
                "&Note=" .     urlencode("Account created via API");
      $api = Poker_API($url,$params,true);
      if ($api["Result"] == "Ok") echo "Account successfully created for $Player";
      else echo "Error: " . $api["Error"] . "<br>Click Back Button to correct.";
      exit;
    }
  ?>

  <h3>Create New Account</h3>
  <form method="post">
    <table>
    <tr><td>Your player name:</td><td><input type="text" name="Player" /></td></tr>
    <tr><td>Your real name:</td><td><input type="text" name="RealName" /></td></tr>
    <tr><td>Your gender:</td><td><input type="radio" name="Gender" Value="Male" checked>Male</input> &nbsp; 
                 <input type="radio" name="Gender" Value="Female">Female</input></td></tr>
    <tr><td>Your location:</td><td><input type="text" name="Location" /></td></tr>
    <tr><td>Select a password:</td><td><input type="password" name="Password1" /></td></tr>
    <tr><td>Confirm password:</td><td><input type="password" name="Password2" /></td></tr>
    <tr><td>Your email address:</td><td><input type="text" name="Email" /></td></tr>
    <tr><td>Your avatar:</td><td>
    <div style="width: 100px; height: 175px; overflow: auto; border: solid 2px">
      <?php
        for ($i=0; $i<$avatarmax; $i++)
        {
          $a = "display: inline-block; width: 48px; height: 48px; background: " . 
               "url('" . $avatarurl . "') no-repeat -" . ($i * 48) . "px 0px;"; 
          $s = "<input type='radio' name='Avatar' value='" . ($i + 1) . "'";
          if ($i == 0) $s .= " checked";
          $s .= ">"; 
          $s .= "<div style=\"" . $a . "\"></div>";
          echo $s . "<br><br>\r\n";
        }
      ?>
    </div>
    </td></tr>
    </table>
    <input type="submit" name="Submit" value="Submit" />
  </form>

</body>
</html>

Export Accounts

This is a PHP example (AccountsCSV.php) for exporting the player accounts list to a comma-separated-values (CSV) file which can then be imported directly into a spreadsheet application:


<html>
<body>

<?php

  include "API.php";  // API $url and $pw values set here

  // Fetch the list of players using the AccountsList API command.

  $params = "Password=" . $pw . "&Command=AccountsList&Fields=Player,Title,Level,RealName," . 
    "PW,Gender,Location,Email,Balance,RingChips,Avatar,AvatarFile,Logins,LastReset,FirstLogin," . 
    "LastLogin,ValCode,Chat,ChatColor1,ChatColor2,Custom,Note";
  $api = Poker_API($url,$params,true);
  $result = $api["Result"];
  if ($result == "Error") die("Error: " . $api["Error"]);

  // iterate through list

  echo "<pre>\r\n";
  echo '"Player","Title","Level","RealName","Password","Gender","Location","Email",' .
       '"Balance","RingChips","Avatar","AvatarFile","Logins","LastReset","FirstLogin",' . 
       '"LastLogin","ValCode","Chat","ChatColor1","ChatColor2","Custom","Note"' . "\r\n";
  for ($i=1; $i<=$api["Accounts"]; $i++)
  {
    echo '"' . $api["Player" .$i] . '",';
    echo '"' . $api["Title" .$i] . '",';
    echo '"' . $api["Level" .$i] . '",';
    echo '"' . $api["RealName" .$i] . '",';
    echo '"' . $api["PW" .$i] . '",';
    echo '"' . $api["Gender" .$i] . '",';
    echo '"' . $api["Location" .$i] . '",';
    echo '"' . $api["Email" .$i] . '",';
    echo '"' . $api["Balance" .$i] . '",';
    echo '"' . $api["RingChips" .$i] . '",';
    echo '"' . $api["Avatar" .$i] . '",';
    echo '"' . $api["AvatarFile" .$i] . '",';
    echo '"' . $api["Logins" .$i] . '",';
    echo '"' . $api["LastReset" .$i] . '",';
    echo '"' . $api["FirstLogin" .$i] . '",';
    echo '"' . $api["LastLogin" .$i] . '",';
    echo '"' . $api["ValCode" .$i] . '",';
    echo '"' . $api["Chat" .$i] . '",';
    echo '"' . $api["ChatColor1" .$i] . '",';
    echo '"' . $api["ChatColor2" .$i] . '",';
    echo '"' . $api["Custom" .$i] . '",';
    echo '"' . $api["Note" .$i] . '"';
    echo "\r\n";
  }
  echo "</pre>\r\n";

?>

</body>
</html>

Player Search

This is a PHP example (PlayerSearch.php) that searches for a player via web interface. It displays whether a particular player is currently logged in or not and displays the list of ring game and tournament tables where they are waiting or seated:


<html>
<body>

  <?php
    if (isset($_REQUEST["Search"]))   // page load via search button
    {
      include "API.php";  // include file has $pw, $url, Poker_API()
  
      // get player name submitted by form

      $player = $_REQUEST["Player"];        
      if ($player == "")
      {
        echo "<b>No player name entered. Click the back button.</b><br/>";
        exit;
      } 

      // get list of connections 

      $params = "Password=$pw&Command=ConnectionsList&Fields=Player";
      $api = Poker_API($url,$params,true);
      if ($api["Result"] == "Error")
      {
        echo "Error: " . $api["Error"] . "<br/>";
        exit;
      }

      // loop thru connections list looking for player

      $found = false; 
      $connections = $api["Connections"];
      for ($i = 1; $i <= $connections; $i++)        
      {
        if (strcasecmp($player,$api["Player" . $i])==0)
        {
          $found = true;
          break;
        }
      }
      echo "<b>" . $player . " is ";
      if ($found != true) echo "not ";
      echo "logged in</b><br/>";

      // get list of ring games

      echo "<br/><b>Ring Games</b><br/>";
      $params = "Password=$pw&Command=RingGamesList&Fields=Name";
      $api = Poker_API($url,$params,true);
      if ($api["Result"] == "Error")
      {
        echo "Error: " . $api["Error"] . "<br/>";
        exit;
      }

      // loop thru ring games list looking for player

      $found = 0;
      $rcount = $api["RingGames"];
      for ($i = 1; $i <= $rcount; $i++)
      {

        $r = $api["Name" . $i];


        // fetch waiting
   
        $params = "Password=$pw&Command=RingGamesWaiting&Name=" . urlencode($r);
        $api2 = Poker_API($url,$params,true);
        $pcount = $api2["Count"];

        // loop thru waiting list

        for ($j = 1; $j <= $pcount; $j++)
        {
          if (strcasecmp($player,$api2["Wait" . $j])==0)
          {
            $found++;
            echo "In waiting list for " . $r . "<br/>";
            break;
          }
        }

        // fetch players
 
        $params = "Password=$pw&Command=RingGamesPlaying&Name=" . urlencode($r);
        $api2 = Poker_API($url,$params,true);
        $pcount = $api2["Count"];

        // loop thru players list

        for ($j = 1; $j <= $pcount; $j++)
        {
          if (strcasecmp($player,$api2["Player" . $j])==0)
          {
            $found++;
            echo "Seated at " . $r . "<br/>";
            break;
          }
        }
      }
      if ($found == 0) echo "(none)<br/>";

      // get list of tournaments

      echo "<br/><b>Tournaments</b><br/>";
      $params = "Password=$pw&Command=TournamentsList&Fields=Name";
      $api = Poker_API($url,$params,true);
      if ($api["Result"] == "Error")
      {
        echo "Error: " . $api["Error"] . "<br/>";
        exit;
      }

      // loop thru tournaments list looking for player

      $found = 0;
      $tcount = $api["Tournaments"];
      for ($i = 1; $i <= $tcount; $i++)
      {

        $t = $api["Name" . $i];

        // fetch waiting

        $params = "Password=$pw&Command=TournamentsWaiting&Name=" . urlencode($t);
        $api2 = Poker_API($url,$params,true);
        $pcount = $api2["Count"];

        // loop thru waiting list

        for ($j = 1; $j <= $pcount; $j++)
        {
          if (strcasecmp($player,$api2["Wait" . $j])==0)
          {
            $found++;
            echo "In waiting list for " . $t . "<br/>";
            break;
          }
        }

        // fetch players  

        $params = "Password=$pw&Command=TournamentsPlaying&Name=" . urlencode($t);
        $api2 = Poker_API($url,$params,true);
        $pcount = $api2["Count"];

        // loop thru players list

        for ($j = 1; $j <= $pcount; $j++)
        {
          if (strcasecmp($player,$api2["Player" . $j])==0)
          {
            $found++;
            echo "Seated at " . $t . " - Table " . $api2["Table" . $j] . "<br/>";
            break;
          }
        }
      }
      if ($found == 0) echo "(none)<br/>";
      echo "<br/><b>Click the back button for another search</b><br/>";
      exit;
    }
  ?>

  <!-- display input form on first load  -->
  <!-- post results back to same page -->

  <h3>Search for Player</h3>
  <form method="post">
    Screen name:<br/>
    <input type="text" name="Player" />
    <input type="submit" name="Search" value="Search" />
  </form>

</body>
</html>

Tournament Results

This is a PHP example (TourneyResults.php) that displays a droplist of tournament result files and then retrieves the one selected:


<html>
<body>
<?php

  include "API.php";  // $url, $pw, and Poker_API() defined here

  if (isset($_REQUEST["submit"]))  // page loaded via submit button
  {

    // split out date and tourney name from selected value

    $tourney = stripslashes($_REQUEST["tourney"]);

    $trdate = Substr($tourney,0,10);
    $trname = Substr($tourney,12);

    // retrieve file

    $params = "Password=" . $pw . "&Command=TournamentsResults&Date=" . $trdate . "&Name=" . urlencode($trname);
    $api = Poker_API($url,$params,false);

    // check for error

    if ($api[0] == "Result=Error") die($api[1]);

    // display results

    echo "<pre>\n";
    for ($i=1; $i<count($api); $i++) echo $api[$i] . "\n";
    echo "</pre>\n"; 
  }
  else   // initial page load
  {

    // retrieve list of files

    $params = "Password=" . $pw . "&Command=TournamentsResults";
    $api = Poker_API($url,$params,true);

    // check for error

    if ($api["Result"] == "Error") die($api["Error"]);

    // fill droplist with dates and tourney names

    $count = $api["Files"];
    echo "<h3>Tournament Results</h3>\n";
    echo "<form method='post'>\n";
    echo "<select name='tourney'>\n";
    for ($i=1; $i<=$count; $i++)
    {
      $tr = $api["Date" . $i] . "  " . htmlspecialchars($api["Name" . $i],ENT_QUOTES);
      echo "<option value='" . $tr . "'>" . $tr . "</option>\n";
    } 

    // submit button reloads page with selected file info

    echo "</select> &nbsp; <input type='submit' name='submit' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p>\n";
  }

?>
</body>
</html>

Hand History

This is a PHP example (HandHistory.php) that displays a droplist of hand history files and then retrieves the one selected:


<html>
<body>
<?php

  include "API.php";  // $url, $pw, and Poker_API() defined here

  if (isset($_REQUEST["submit"]))  // page loaded via submit button
  {

    // split out date and table name from selected value

    $history = stripslashes($_REQUEST["history"]);
    $hhdate = Substr($history,0,10);
    $hhname = Substr($history,12);  

    // retrieve file

    $params = "Password=" . $pw . "&Command=LogsHandHistory&Date=" . $hhdate . "&Name=" . urlencode($hhname);
    $api = Poker_API($url,$params,false);

    // check for error

    if ($api[0] == "Result=Error") die($api[1]);

    // display results

    echo "<pre>\n";
    for ($i=1; $i<count($api); $i++) echo $api[$i] . "\n";
    echo "</pre>\n"; 
  }
  else   // initial page load
  {

    // retrieve list of files

    $params = "Password=" . $pw . "&Command=LogsHandHistory";
    $api = Poker_API($url,$params,true);

    // check for error

    if ($api["Result"] == "Error") die($api["Error"]);

    // fill droplist with dates and table names

    $count = $api["Files"];
    echo "<h3>Hand History</h3>\n";
    echo "<form method='post'>\n";
    echo "<select name='history'>\n";
    for ($i=1; $i<=$count; $i++)
    {
      $hh = $api["Date" . $i] . "  " . htmlspecialchars($api["Name" . $i],ENT_QUOTES);
      echo "<option value='" . $hh . "'>" . $hh . "</option>\n";
    } 

    // submit button reloads page with selected file info

    echo "</select> &nbsp; <input type='submit' name='submit' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p>\n";
  }

?>
</body>
</html>

Error and Event Logs

This is a PHP example (Logs.php) that displays a droplist of error log and event log files and then retrieves the one selected:

<html>
<body>
<?php

  include "API.php";  // $url, $pw, and Poker_API() defined here

  if (isset($_REQUEST["errors"]))  // retrieve selected error log
  {
    $edate = $_REQUEST["edate"];
    $params = "Password=" . $pw . "&Command=LogsError&Date=" . $edate;
    $api = Poker_API($url,$params,false);
    if ($api[0] == "Result=Error") die($api[1]);
    echo "<pre>\n";
    for ($i=1; $i<count($api); $i++) echo $api[$i] . "\n";
    echo "</pre>\n"; 
  }
  elseif (isset($_REQUEST["events"]))  // retrieve selected event log
  {
    $edate = $_REQUEST["edate"];
    $params = "Password=" . $pw . "&Command=LogsEvent&Date=" . $edate;
    $api = Poker_API($url,$params,false);
    if ($api[0] == "Result=Error") die($api[1]);
    echo "<pre>\n";
    for ($i=1; $i<count($api); $i++) echo $api[$i] . "\n";
    echo "</pre>\n"; 
  }
  else  // initial page load - display selection droplists
  {

    // retrieve error log dates

    $params = "Password=" . $pw . "&Command=LogsError";
    $api = Poker_API($url,$params,true);
    if ($api["Result"] == "Error") die($api["Error"]);
    $count = $api["Files"];
    echo "<h3>Error Logs</h3>\n";

    // display droplist of dates

    echo "<form method='post'>\n";
    echo "<select name='edate'>\n";
    for ($i=1; $i<=$count; $i++)
    {
      $e = $api["Date" . $i];
      echo "<option value='" . $e . "'>" . $e . "</option>\n";
    } 

    // submit button reloads page with selected error log date

    echo "</select> &nbsp; <input type='submit' name='errors' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p><br/>\n";


    // retrieve event log dates

    $params = "Password=" . $pw . "&Command=LogsEvent";
    $api = Poker_API($url,$params,true);
    if ($api["Result"] == "Error") die($api["Error"]);
    $count = $api["Files"];
    echo "<h3>Event Logs</h3>\n";

    // display droplist of dates

    echo "<form method='post'>\n";
    echo "<select name='edate'>\n";
    for ($i=1; $i<=$count; $i++)
    {
      $e = $api["Date" . $i];
      echo "<option value='" . $e . "'>" . $e . "</option>\n";
    } 

    // submit button reloads page with selected event log date

    echo "</select> &nbsp; <input type='submit' name='events' value='Submit'>\n";
    echo "</form>\n";
    echo "<p>Files found: " . $count . "</p>\n";
  }

?>
</body>
</html>

Login Stats

This is a PHP example (LoginStats.php) that parses through one or more event logs and calculates login statistics. Specifically, for each period of server up time, it displays the total number of logins, unique logins, and peak logins. The total logins is just a simple count of logins. If a single player logged in and out 10 times then 10 is added to that count. The unique count is the number of different players that logged in at least once during that time. The peak count is the maximum number of players that were all logged in at the same time. Note: if you see a "?" in the Start or Stop column of the output, that means the server went online or offline outside of the date range selected (a new log file is started when the clock passes midnight). In your log settings, you must have "Log system events", "Log login events", and "Log logout events" all set to "Yes" to use this function.


<html>
<body>
<?php

  include "API.php";  // $url, $pw, and Poker_API() defined here

  // retrieve event log dates into $edates array

  $params = "Password=" . $pw . "&Command=LogsEvent";
  $api = Poker_API($url,$params,true);
  if ($api["Result"] == "Error") die($api["Error"]);
  $count = $api["Files"];
  $edates = array();
  for ($i=1; $i<=$count; $i++) $edates[] = $api["Date" . $i];
 
  if (isset($_REQUEST["stats"]))  // parse selected event logs
  {

    function addrecord()  // add a record representing a server session
    {
      global $playerlist, $starttime, $stoptime, $logins, $peaklogins;
      $unique = count($playerlist); 
      echo "<tr><td>$starttime</td><td>$stoptime</td><td>$logins</td><td>$unique</td><td>$peaklogins</td></tr>\n";
    }

    function resetstats()  // new session
    {
      global $playerlist, $starttime, $stoptime, $logins, $currentlogins, $peaklogins;
      $logins = 0;
      $currentlogins = 0; 
      $peaklogins = 0;
      $playerlist = array();
      $starttime = "?";
      $stoptime = "?";
    }

    $edate1 = $_REQUEST["edate1"];
    $edate2 = $_REQUEST["edate2"];
    if ($edate1 > $edate2) die("Starting date is greater than ending date. Click the Back button.");

    // loop through event files and merge into $events array

    $events = array(); 
    for ($i=$edate1; $i<=$edate2; $i++)
    {
      $params = "Password=" . $pw . "&Command=LogsEvent&Date=" . $edates[$i];
      $api = Poker_API($url,$params,false);
      if ($api[0] == "Result=Error") die($api[1]);
      array_shift($api);  // remove "Result=Ok" in record 0
      foreach ($api as $line) $events[] = $line;
    }

    resetstats();
    $title = "Logins " . $edates[$edate1];
    if ($edate1 != $edate2) $title = $title . " to " . $edates[$edate2];
    echo "<table cellpadding='5' border='1'>\n";
    echo "<tr><th colspan='5'>$title</th></tr>\n";
    echo "<tr><th>Start</th><th>Stop</th><th>Logins</th><th>Unique</th><th>Peak</th></tr>\n";

    // loop through event lines

    foreach ($events as $line)
    {
      if (strpos($line,"System|Game server started") !== false)
      {
        $starttime = substr($line,0,19);
      }
      elseif (strpos($line,"System|Game server stopped") !== false)
      {
        $stoptime = substr($line,0,19);
        addrecord();
        resetstats();
      }
      elseif (strpos($line," logged into session ") !== false)
      {
        $logins++;
        $currentlogins++;
        if ($currentlogins > $peaklogins) $peaklogins = $currentlogins;
        $p = strpos($line," logged into session ");
        $player = substr($line,26,$p-26);
        $playerlist[$player] = true;
      }
      elseif (strpos($line," logged out") !== false)
      {
        if ($currentlogins > 0) $currentlogins--;
      }
    }
    if ($starttime != "?") addrecord();
    echo "<tr><th colspan='5'>Records parsed: " . count($events) . "</th></tr>\n";
    echo "</table><br/>\n";
  }
  else  // initial page load - display selection droplists
  {

    echo "<h3>Login Stats</h3>\n";
    echo "<p>Event files found: " . $count . "</p>\n";

    // display droplist of dates

    echo "<form method='post'>\n";
    echo "Start date: <select name='edate1'>\n";
    foreach ($edates as $i => $e) echo "<option value='" . $i . "'>" . $e . "</option>\n";
    echo "</select> &nbsp; ";

    echo "End date: <select name='edate2'>\n";
    foreach ($edates as $i => $e) echo "<option value='" . $i . "'>" . $e . "</option>\n";
    echo "</select> &nbsp; ";

    // submit button reloads page with selected event log dates

    echo "<input type='submit' name='stats' value='Submit'>\n";
    echo "</form>\n";
  }

?>
</body>
</html>

Automated Logins

These are various examples that show how to automate logins from an external web page. This first one (Login.htm) is just a simple HTML form (no scripting) where the user inputs their own login name and password and it is forwarded to the player client directly. Replace the URL in the action parameter with your own. Remove the target parameter if you want the poker client to load into the current window instead of spawning a new one.

<html>
<body>
<h3>Poker Login</h3>
<form method="post" action="http://127.0.0.1:8087" target="_blank">
  <table>
    <tr>
      <td>Player Name:</td>
      <td><input type="text" name="LoginName"></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input type="password" name="LoginPassword"></td>
    </tr>
    <tr>
      <th colspan="2"><input type="submit" value="Login"></th>
    </tr>
  </table>
</form>
</body>
</html>

This next example (SessionKey.php) uses the API to confirm the player's password (version 2 method) in the background and then logs them in using a Session Key. See next example for the version 3/4 method.

<?php

  $server = "http://127.0.0.1:8087";   // set your url here
  include "API.php";  // $pw and $url set in this file

  if (isset($_POST["Login"]))
  {
    $player = $_POST["Player"];
    $password = $_POST["Password"];
    $params = "Password=$pw&Command=AccountsGet" . "&Player=" . urlencode($player);
    $api = Poker_API($url,$params,true);
    if ($api["Result"] != "Ok") die($api["Error"] . "<br/>" . "Click Back Button to retry.");
    if ($api["PW"] != $password) die("Password is incorrect. Click Back Button to retry.");
    $params = "Password=$pw&Command=AccountsSessionKey&Player=" . urlencode($player);
    $api = Poker_API($url,$params,true);
    if ($api["Result"] != "Ok") die($api["Error"] . "<br/>" . "Click Back Button to retry.");
    $key = $api["SessionKey"];
    header("Location:" . $server . "/?LoginName=" . $player . "&SessionKey=" . $key);
    exit;
  }
?>

<html>
<body>
 
  <h3>Poker Login</h3>
  <form method="post">
    <table>
      <tr>
        <td>Player Name:</td>
        <td><input type="text" name="Player"></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="Password"></td>
      </tr>
      <tr>
        <th colspan="2"><input type="submit" name="Login" value="Login"></th>
      </tr>
    </table>
  </form>

</body>
</html>

Player passwords are encrypted in version 3 and 4 so the previous example will not work there. Instead you can use the new AccountsPassword command to verify the player's password as shown here (SessionKey3.php):

<?php

  $server = "http://127.0.0.1:8087";   // set your url here
  include "API.php";  // $pw and $url set in this file

  if (isset($_POST["Login"]))
  {
    $player = $_POST["Player"];
    $password = $_POST["Password"];
    $params = "Password=$pw&Command=AccountsPassword" . "&Player=" . urlencode($player) . "&PW=" . urlencode($password);
    $api = Poker_API($url,$params,true);
    if ($api["Result"] != "Ok") die($api["Error"] . "<br/>" . "Click Back Button to retry.");
    if ($api["Verified"] != "Yes") die("Password is incorrect. Click Back Button to retry.");
    $params = "Password=$pw&Command=AccountsSessionKey&Player=" . urlencode($player);
    $api = Poker_API($url,$params,true);
    if ($api["Result"] != "Ok") die($api["Error"] . "<br/>" . "Click Back Button to retry.");
    $key = $api["SessionKey"];
    header("Location:" . $server . "/?LoginName=" . $player . "&SessionKey=" . $key);
    exit;
  }
?>

<html>
<body>
 
  <h3>Poker Login</h3>
  <form method="post">
    <table>
      <tr>
        <td>Player Name:</td>
        <td><input type="text" name="Player"></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td><input type="password" name="Password"></td>
      </tr>
      <tr>
        <th colspan="2"><input type="submit" name="Login" value="Login"></th>
      </tr>
    </table>
  </form>

</body>
</html>