Building A Document Request Protocol Part 2/2 - Using SARP commands
(Page 4 of 6 )
Now that we're connected and authenticated, we can use any of the SARP commands that we defined in the first article to retrieve the details of the brands and cars in our articles database. If your memory needs refreshing, here are the commands we've defined for our SARP server to respond to:
- LOGIN [user] [password]
- LIST CATEGORIES
- LIST ARTICLES [category name]
- ADD CATEGORY [category name]
- ADD ARTICLE [title] [content] [price] [category id]
Let's retrieve the list of categories from our SARP server (remember that when our SARP server receives this command, it uses a select * from categories query to retrieve the category names from our Access database and forms them into a single string variable):
fputs($sarpHandle, "LIST CATEGORIES");
$sarpData = fgets($sarpHandle, 1024);The data returned into the $sarpData variable looks like this:
1|BMW;4|Lamborghini;2|Mercedes-Benz;3|Porsche;
We can use PHP's explode function to separate each categories ID and name into an array:
$cats = explode(";", $sarpData);
echo "<h3>Available Content:</h3>";Now that each category is separated into a separated index in the $cats array, we can loop through each category and use the "LIST ARTICLES" SARP command to retrieve the details of each article stored in our SARP servers Access database:
for($i = 0; $i < sizeof($cats) - 1; $i++)
{
$catFields = explode("|", $cats[$i]);
echo $catFields[1] . "<br>";
fputs($sarpHandle, "LIST ARTICLES " . $catFields[0]);
$sarpData = fgets($sarpHandle, 1024);
$arts = explode(";", $sarpData);
echo "<ul>";
for($j = 0; $j < sizeof($arts) - 1; $j++)
{
$art = explode("|", $arts[$j]);
echo "<li><b>" . $art[1] . "</b><br>";
echo $art[2] . "<br><b>Price: $" . number_format($art[3], 2, ".", ",");
echo "</b></li>";
}
echo "</ul>";
}The output from the code above looks like this:

Next: Adding new content using SARP commands >>
More PHP Articles
More By Mitchell Harper