1. I have corrected the e-mail settings so that outgoing e-mails from these forums should be sent now. If you tried to Register or Reset your Password, please try again!
    Dismiss Notice

A quick sample PHP script

Discussion in 'Census: General Discussion' started by Lantis, Dec 12, 2011.

  1. Lantis

    Lantis Guest

    For those interested in parsing the feeds using PHP, here is a short script that I wrote to make it easy for RMSA administrators to find their Guild ID.  It is a good example on how simple it can be to parse the XML feeds using PHP and the SimpleXML module.

    -----

    <html> <head><title>Guild search</title><meta content="text/html; charset=utf-8" http-equiv="content-type"></head><body style="text-align:left; font-size: normal;">


    <?php

    if (IsSet($_POST['guildname'])) { $guildname = strtolower($_POST['guildname']);} else { $guildname='';}

    if (IsSet($_POST['servername'])) { $servername = $_POST['servername'];} else { $servername='';}

    if (IsSet($_POST['rmmatch'])) {

    switch ($_POST['rmmatch']) { case "partial": $guildnamequery = $guildname; $rmmatchset_partial=" checked "; break; case "starts": $guildnamequery = "^$guildname"; $rmmatchset_starts=" checked "; break; case "exact": $guildnamequery = "^$guildname$"; $rmmatchset_exact=" checked "; break; }

    } else { $guildnamequery = $guildname; $rmmatchset_partial=" checked ";}

    $serverlist = '<select name="servername" tabindex="10">' .'<option value="Antonia Bayle">Antonia Bayle</option>' .'<option value="Barren Sky">Barren Sky</option>' .'<option value="Butcherblock">Butcherblock</option>' .'<option value="Crushbone">Crushbone</option>' .'<option value="Everfrost">Everfrost</option>' .'<option value="Freeport">Freeport</option>' .'<option value="Guk">Guk</option>' .'<option value="Harla Dar">Harla Dar</option>' .'<option value="Nagafen">Nagafen</option>' .'<option value="Oasis">Oasis</option>' .'<option value="Permafrost">Permafrost</option>' .'<option value="Sebilis">Sebilis</option>' .'<option value="Splitpaw">Splitpaw</option>' .'<option value="Storms">Storms</option>' .'<option value="The Bazaar">The Bazaar</option>' .'<option value="Unrest">Unrest</option>' .'<option value="Valor">Valor</option>' .'<option value="Vox">Vox</option>' .'</select>';

    $serverlist = Str_replace("value="$servername"", "value="$servername" selected",$serverlist);

    ?>

    Search Guild ID


    <form method="post" action="<?php echo $_SERVER['REQUEST_URI'];?>" name="Server Search"><table cols='3' border='0'>

    <tr style="border: none;"><td style="border: none;">Guild Name:</td><td colspan="2" style="border: none;"><input size="40" tabindex ="5" name="guildname" value="<?php echo $guildname;?>"></td></tr><tr style="border: none;"><td style="border: none;">Server Name:</td><td style="border: none;"> <?php echo $serverlist; ?></td> <td style="border: none; text-align:right;"><input name="rmsearch" value="Search" tabindex="15" type="submit"></td></tr><tr><td style="border: none;">Match:</td><td style="border: none; text-align: left; font-size: smaller;" colspan="2"> <input name="rmmatch" value="partial" type="radio" <?php echo $rmmatchset_partial;?> >Partial <input name="rmmatch" value="starts" type="radio" <?php echo $rmmatchset_starts;?> >Starts with <input name="rmmatch" value="exact" type="radio" <?php echo $rmmatchset_exact;?> >Exact</td></tr>

    </table>

    </form>

    <?php

    if ((IsSet($_POST['guildname'])) && ($_POST['guildname'] != "")) {

    echo "<div style="color: green;">Searching..."; $query = "census.daybreakgames.com/".$identifystring."xml/get/eq2/guild/?name_lower=/".urlencode($guildnamequery)."/&world=".urlencode($_POST['servername'])."&c:show=id,name&c:sort=name_lower:1&c:count=1&c:limit=10"; $xml = fetch_URL($query,0);

    try { $guildobj = NEW SimpleXMLElement($xml); } catch(Exception $e) { echo "<span style='color: red;'>Parsing error!
    ",$e->getMessage()." in ".basename($e->getFile()).":".$e->getLine()."!
    "; exit; }

    if ($guildobj['count'] > 10) { echo "<span style='color: orange;'>".$guildobj['count']." results found - listing only ".$guildobj['returned']."
    "; } elseif ($guildobj['returned'] == 0) { echo "<span style='color:red;'>No guild found!
    "; exit; } else { echo $guildobj['count']." result(s) found:
    "; }

    echo "<table cols='2' border='0'><thead><tr style='text-align:left; font-size:normal;'><th>Name</th><th>Guild ID</th></tr></thead>"; foreach ($guildobj as $guild) { echo "<tr style='font-size: smaller;'><td style='border: none;'>".$guild['name']."</td><td style='border: none;'><input readonly type='text' value='".$guild['id']."'></td></td>"; } echo "</table>";}

    ?>

    </body></html>


    -----


    SimpleXML will turn the XML content into an object.  You can then reference elements using ->, and attributes using ['name'].  Example:

    echo $guildobj->guild['name'];

    will give you the (first) guild's name.








     
  2. Lantis

    Lantis Guest

    I updated the first post to take advantage of the new regexp support (Thanks Dan!).

     
  3. DanKinney

    DanKinney Guest

    Awesome!

    It just changed slightly...could you make sure it still works?  See the 3rd post in this thread.

    -dan

     
  4. Lantis

    Lantis Guest

    I can't make the new syntax work (tried both with "/" and "%2F".  Here's my test query:

    Code:
    census.daybreakgames.com/xml/get/eq2/guild/?name=i/legion/&worldid=104&c:limit=3&c:show=name&c:sort=name
     
  5. DanKinney

    DanKinney Guest

    It works now. :)

    -dan

     
  6. Lantis

    Lantis Guest

    Awesome, thanks :)  Updating OP in a sec.

     
  7. Lantis

    Lantis Guest

    I rewrote a good bit of the guildsearch code in RosterMaster to take advantage of newer features such as c:count=1 and the new name_lower attribute.  I updated the original post to take these into account (means only one query is now send instead of two, and results are much faster).

    There's also a new radio field that lets you switch between various matching methods: partial match, exact match, or starts with.  That way, the user does not need to know regexp to do more accurate searches.

    (sorry for the messy formatting, I haven't found a way to do a pre-formatted block on these forums)

     

Share This Page