Sunday, November 06, 2011

IRC - Get the user list of a channel in JSON with Denora/PHPDenora

You need to have Denora and PHPDenora installed and running on your IRCd for this to work.

<?php
define("_VALID_PARENT", 1);
require ("libs/phpdenora/init.php");  //You have to provide the proper path to the file here
$link = sql_db_connect();
echo json_encode(denora_who_in_channel(mysql_real_escape_string(trim($_GET["channel"], '"'))));
sql_db_close($link);
?>

It works like this : http://stats.yournetwork.net/userlist.php?channel=%23yourchannel

In PHP, to get the file content and parse it into an array, you will need to do something like this :
$userlist = json_decode(file_get_contents("http://stats.yournetwork.net/userlist.php?channel=%23yourchannel"), true);

To loop through the array, you will need to follow this logic :
foreach ($userlist as $userinfo) {
       foreach ($userinfo as $infokey => $infovalue) {
                //echo $infokey . " : " . $infovalue . ", ";
       }
       //echo "\n";
}

If you want to access only a single attribute per user, like for example, if you only want to list the nicknames, you can access the attribute directly by its name by doing something like this :
foreach ($userlist as $userinfo) {
       echo $userinfo["nick"];
}


See also : http://whatnwhat.blogspot.com/2011/11/irc-get-user-count-of-channel-in-plain.html

No comments:

Post a Comment