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.
Building A Document Request Protocol Part 2/2 - Adding new content using SARP commands (Page 5 of 6 )
We can also use PHP's socket functions and our custom SARP commands to add new categories and articles to our SARP servers Access database. We can add a new category using the "ADD CATEGORY" SARP command, like this:
// Add a category
fputs($sarpHandle, "ADD CATEGORY Jaguar");
$sarpData = fgets($sarpHandle, 1024);
echo $sarpData;
When our SARP server receives the "ADD CATEGORY Jaguar" command, it will add a new category called Jaguar to the categories table of our access database, and return a 104 status message, like this:
Now that we've added Jaguar to the categories table, let's add the Jaguar XK8 to the articles table:
Because each variable passed to the ADD ARTICLE command is separated by a space, you must replace all spaces in the title and content fields with underscores. The underscores are replaced with spaces when the SARP server processes the command.
If we retrieve a list of categories and articles again as we did earlier, then we would see that Jaguar and the XK8 have been added to our Access database:
Our SARP server has been designed so that if it receives an unknown command, it will not fail over, but rather return one single new line character and no other data:
fputs($sarpHandle, "BLAH BLEE SCHMEE");
$sarpData = fgets($sarpHandle, 1024);
echo $sarpData;
In the code above, $sarpData would contain a single new line character, and no errors would occur.
Once we're done communicating with our SARP server, we close the socket connection using the fclose function: