Ryan has posted 9 posts at DZone. View Full User Profile

Integrating PHP with Flash Collaboration Services using REST

12.03.2009
| 21306 views |
  • submit to reddit

In part one of integrating PHP and LiveCycle Collaboration Services (LCCS) we focused on how you could use PHP to add external authentication to your LCCS application. Another great way to integrate LCCS and PHP is with the REST APIs and the library that ships with the LCCS SDK. With those APIs we can perform a number of management functions including creating and deleting rooms, managing roles, and creating templates. These APIs make it very easy to create a customized, dynamic collaboration experience with PHP. You can create rooms on the fly for your users and get information about what kind of content is in those rooms. In this example we're going to create a basic room manager with PHP that will let us create, delete, and view information about specific rooms.

Similarly to the first part, we need to provide login credentials to AFCS and log in before we can make any calls against the API. We need to import the afcs.php libraries that are provided with the SDK and then create a new instance of AFCSAccount with our credentials. Once that's finished we'll call the listRooms() method to get an array of rooms that our account has on the server.

<?php
require_once("afcs.php");
$title = "LiveCycle Collaboration Services Room Manager";

$username = "Your username here";
$password = "Your password here";
$account = "Your account name here";

$account_url = "http://connectnow.acrobat.com/{$account}";

session_start();

$afcs_account = new AFCSAccount($account_url);
$afcs_account->login($username,$password);
$arr_rooms = $afcs_account->listRooms();

?>

Once we have a list of rooms, we'll list those in a table so we can start creating a management console. This is just a very basic example so we will loop through the array and output the information to a table. Each object in the array is of type AFCSItem which is defined in the afcs.php file. It defines a name, a description, and a created date so we can use those as the headings for our table. Then we'll just use some basic PHP to loop through the array and populate our HTML table.

<html>
<head>
<title><?php print $title ?></title>
</head>
<body>
<h2>Room List</h2>
<table>
<tr>
<td><strong>Room Name</strong></td>
<td><strong>Description</strong></td>
<td><strong>Creation Date</strong></td>
<td></td>
</tr>
<?php

for( $i = 0; $i < count($arr_rooms); $i++)
{
$item = $arr_rooms[$i];
$date_time = $item->created;
print "<tr><td>{$item->name}</td>";
print "<td>{$item->desc}</td>";
print "<td>{$date_time->format("r")}</td>";
}

?>
</table>
</body>
</html>

 

Now we have a basic room list for all the rooms that are associated with our account. The next step is to be able to create rooms. The afcs.php library with the SDK also includes a createRoom() method that takes a room name and a template. Templates in LCCS allow you to create rooms with customized settings. I won't cover using templates in this tutorial but you can look at the LCCS documentation to see how you would use templates in your room management. Then it's easy to use these APIs to both create templates and create new rooms with types of templates. For the room creation code we'll attach a form to the end with a text box, a hidden field, and a submit button that lets us create a room with a specific name. Just add the following code after the end table tag.

<h2>Create a Room</h2>
<form method="POST">
<input type="text"name="room_name"/>
<input type="hidden"name="is_creating"value="true"/>
<input type="submit"name="submit"value="Create Room"/>
</form>

We also need to add a function to our PHP code to check to see if we've submitted a form that is creating  a room and then call the creation APIs. The following code uses the AFCSAccount instance we set up earlier to create a room with the name from our form. Place it after the listRooms() function in the php tags.

// We are creating a new room
if (isset($_REQUEST['is_creating']))
{
$creating_room_name = $_REQUEST['room_name'];
$afcs_account->createRoom($creating_room_name);
print "<p>Room Created!</p>";
}

In addition to viewing and creating rooms, it would also be nice if we could get more information about rooms and also delete rooms. In order to accomplish this we'll add some code to our HTML table that adds a delete button and also lets us click on the name of a room to get more information about it. The getRoomInfo() function lets us get a bunch of information about what's going on in the room. We can see information about the server we're on, how many users are in the room, and the history of messages and data that have been sent back and forth. It comes back as an XML file so in the PHP code below I do some parsing to set it up and make it a bit easier to access from the code that displays the information. This code goes below the creation script we just added. We're going to pass the room name in the URL so before any code runs we see if that variable has been set.

// Sets up looking at information for a specific room
$room_name = "";
$room_info = "";

if (isset($_REQUEST['room']))
{
$room_name = $_REQUEST['room'];
$title = "Details for room: {$room_name}";
$room_info_xml = $afcs_account->getRoomInfo($room_name);

$parser = xml_parser_create_ns();
xml_parse_into_struct($parser,$room_info_xml,$room_info);
}

We also want to create the code that will let us delete rooms. Like the createRoom() method, we have a deleteRoom() method that takes a room name and deletes that room from the server. We will modify our HTML table and make it part of a form with buttons that let us click to delete rooms so this part of the PHP script checks to see if the hidden input field, is_deleting, is set before the delete code runs. Each button has the value of "Delete " and then the room name so before we delete the room we use a strstr() method to pull the room name out.

if (isset($_REQUEST['is_deleting']))
{
$deleting_room_name = strstr($_REQUEST['submit']," ");
$afcs_account->deleteRoom($deleting_room_name);
print "Room {$deleting_room_name} deleted.";
}

Now that the scripts for calling the API are finished, we need to modify our HTML table to include the new features. We'll wrap it in a form tag and also add a link to each room name which reloads the page with a room variable in the URL string so we can see the details of that room. The only thing that is a little bit strange is how we get the information. I wanted to make dealing with the XML as straight forward as possible so I just used the xml_parse_into_struct() method to turn it into an array of arrays. Then I just looked at the raw array to figure out which index of the array I needed for a particular bit of information. That's the reason for the extra code below for printing out the room information. Replace the original code between the table tags with this code to enable a detail view and the ability to delete rooms.


<table>
<form method="POST">
<tr>
<td><strong>Room Name</strong></td>
<td><strong>Description</strong></td>
<td><strong>Creation Date</strong></td>
<td></td>
</tr>
<?php
for( $i = 0; $i < count($arr_rooms); $i++)
{
$item = $arr_rooms[$i];
$date_time = $item->created;

print "<tr><td><a href="index.php?room={$item->name}">{$item->name}</a></td>";
print "<td>{$item->desc}</td>";
print "<td>{$date_time->format("r")}</td>";
print "<td><input type='submit' name='submit' value='Delete {$item->name}'/></tr>";

if (isset($_REQUEST['room']))
{
if( $room_name == $item->name)
{
print "<tr><td>";
print "<strong>Is Connected?</strong> {$room_info[8]['value']}<br />";
print "<strong>Number of Users:</strong> {$room_info[9]['value']}<br />";
print "<strong>Total Bytes Uploaded:</strong> {$room_info[13]['value']}<br />";
print "<strong>Total Bytes Downloaded:</strong> {$room_info[14]['value']}<br />";
print "<strong>Total Messages:</strong> {$room_info[15]['value']}<br />";
print "</td></tr>";
}
}
}
?>
<input type="hidden"name="is_deleting" value="true"/>
</form>
</table>

With that, you now have a couple of real-world examples for using your PHP skills to create and manage collaborative applications with LiveCycle Collaboration Services. Part one went over how to use external authentication with LCCS so you can manage users and roles as part of your existing system and have those carry over to your collaborative Flex application. Part two provided information on how you can manage and create rooms for use with your collaborative application using the REST APIs that LCCS makes available. For more information on LiveCycle Collaboration Services and how it can make your web applications real-time you can check out the LiveCycle Collaboration Developer Center and this getting started article.

 

Published at DZone with permission of its author, Ryan Stewart.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)