Home arrow PHP arrow Page 4 - Building A Document Request Protocol Part 2/2
PHP

Building A Document Request Protocol Part 2/2


In this article, Mitchell shows us how to implement our custom SARP protocol that we created yesterday. He uses PHP and its various socket functions to send SARP commands to our SARP server to both retrieve articles and add new articles to the servers Access database.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
February 24, 2002
TABLE OF CONTENTS:
  1. · Building A Document Request Protocol Part 2/2
  2. · Connecting to our SARP server
  3. · Logging in
  4. · Using SARP commands
  5. · Adding new content using SARP commands
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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:

Retrieving categories and articles from our SARP server
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials