PROTO Server Documentation
From DocMGR
Contents |
Notes About Usage in the API
In current implementations, PROTO is initialized in the header file as $PROTO and simply accessed by the rest of the API. Any responses required to be sent in something other that PROTO_DEFAULT are achieved by calling PROTO->setProtocol($NEWMODE) before sending the output. The API automatically calls the “output” method, so current additions to the API need not also call output.
Usage
Example 1: Returning an account list table
//init the class
$PROTO = new PROTO();
//get the list of accounts
$sql = "SELECT * FROM auth_accounts ORDER BY name";
$list = $DB->fetch($sql);
//dump the records as is into PROTO
for ($i=0;$i<$list["count"];$i++)
{
$PROTO->add($list[$i]);
}
//output them in the default format to the browser
$PROTO->output();
/***********/
This would show something like (in XML mode)
<account>
<id>1</id>
<name>John Smith</name>
</account>
Example 2: Return the list w/ a sub list.
//init the class
$PROTO = new PROTO();
//get the list of accounts
$sql = "SELECT * FROM auth_accounts ORDER BY name";
$list = $DB->fetch($sql);
//dump the records as is into PROTO
for ($i=0;$i<$list["count"];$i++)
{
//get all groups this account belongs to
$sql = "SELECT id,name FROM groups WHERE account_id='".$list[$i]["id"]."'";
$groupList = $DB->fetch($sql);
//add our matches under the key "group"
$list[$i]["group"] = $groupList[$i];
$PROTO->add($list[$i]);
}
//output them in the default format to the browser
$PROTO->output();
/**************/
This would show something like (in XML mode)
<account>
<id>1</id>
<name>John Smith
<group>
<id>1</id>
<name>Admin</name>
</group>
<group>
<id>2</id>
<name>Users</name>
</group>
</account>
Class Definition
|
Class |
PROTO |
|
Purpose |
initializes class. sets protocol mode |
|
Inputs |
mode (optional) → protocol mode (JSON or XML). defaults to PROTO_DEFAULT |
|
Returns |
class reference |
Class Methods
|
Method |
setProtocol |
|
Purpose |
changes our output format to JSON or XML |
|
Inputs |
mode → “JSON” or “XML” |
|
Returns |
none |
|
Method |
add |
|
Purpose |
adds the key/value pair to the queue for outputting to the browser later |
|
Inputs |
key → name of the value to output value → value to output. Can be string, number, or array |
|
Returns |
none |
|
Method |
encode |
|
Purpose |
converts the queued data into transmission-ready text in XML or JSON format. |
|
Inputs |
none |
|
Returns |
string |
|
Method |
decode |
|
Purpose |
decodes transmission data into an array for access. Assumes it's dealing with the request variables as they are sanitized during the decode |
|
Inputs |
string → string to decode |
|
Returns |
array |
|
Method |
output |
|
Purpose |
outputs encoded information directly to the browser |
|
Inputs |
none |
|
Returns |
none |
|
Method |
getEncoded |
|
Purpose |
gets encoded queue data w/o outputting it to the browser |
|
Inputs |
none |
|
Returns |
string |
|
Method |
getData |
|
Purpose |
returns the data array we have queued for latter output |
|
Inputs |
none |
|
Returns |
array |
|
Method |
clearData |
|
Purpose |
clears the output queue |
|
Inputs |
none |
|
Returns |
none |

