Tuesday Update: Hello, world.

So..where was I supposed to be today?

  • Monday: Rebuild the NAS and install PHP and Apache. This will give me a place to work. Done!
  • Tuesday: Hello world web service. This will let me get my feet wet with building and deploying the service. Done!
  • Wednesday: Architect services. I need to sit down and really think through how to implement this, since this will drive everything else downstream.
  • Thursday: Rethinking database schema. This needs to be done to properly store all of the data. Right now PART has data in CSV files where it uses the stock ticker as the key, but in the dividend database a GUID is used for all records. So I need to re-think how this all ties together.
  • Friday: Rebuild my development environment. I am currently using Eclipse Luna which is more than 5 versions behind. I think some of the issues I am having are due to me running such an old environment, so I’m just going to rebuild from scratch. This isn’t really needed to build services but it will be needed if I want to update PART to leverage the services.

The below is the code I managed to hack together. It isn’t pretty, but it works. Keeping in mind that I’ve never worked with PHP before (Java back in…2001? C++ since then), getting used to the syntax and formatting will take some time.

The intent of this hello world was to accept either GET or POST, and do something based on the results. Deploying to the NAS was as easy as I thought it would be: just copy the helloworld.php file to the web server’s root directory, and off I went.

This was a complete hack job though..not knowing the PHP operators, I used (empty($_POST)) to tell if I was in a GET, but should have said “if $_GET is not empty”, or (!empty$(_GET)). So, a little backwards logic, but it got the job done.

<?php

echo "<!DOCTYPE HTML>";
echo "<html lan=\"en\">";
echo "<title>Hello, world.</title><body>";
echo "php version=" . phpversion() . "<br /><br />";


if (empty($_POST) and empty($_GET))
{
?>

        <form name="searchByPost" method="post">
                POST:

                <select name="category">
                        <option value="corgi">Corgi</option>
                        <option value="boxer">Boxers</option>
                        <option value="poodle">Poodles</option>
                </select>
                <br />
        
                <input type="submit" value="Search by Post" />
        </form>


        <form name="searchByGet" method="get">
                GET:

                <select name="category">
                        <option value="nacho">Nacho</option>
                        <option value="tippy">Tippy</option>
                        <option value="cocoa">Cocoa</option>
                </select>
                <br />
        
                <input type="submit" value="Search by Get" />
        </form>


<?php
}
elseif (empty($_POST)) // if POST is empty, assume this is GET 
{
        echo "Hello, GET.<br />";
        echo "You selected " . $_GET["category"] . "<br />";
}
elseif (empty($_GET)) // if GET is empty, assume this is POST
{
        echo "Hello, POST.<br />";
        echo "You selected " . $_POST["category"] . "<br />";
}
else
{
        echo "Hmm, something horrible happened.  I expected a POST or a GET but found neither";
}       

echo "</body>";
echo "</html>";

?>
</code>

Now, the above just got PHP working.. But what I really wanted to do was a true service. So here it is:

<?php

// check if nothing was passed via POST, solicit input from user.
if (empty($_POST))
{
echo "<!DOCTYPE HTML>";
echo "<html lan=\"en\">";
echo "<title>Hello, service.</title><body>";
echo "php version=" . phpversion() . "<br /><br />";

?>

        

        <form name="searchByPost" method="post">
                Search for what?

                <select name="category">
                        <option value="names">Names of pets</option>
                        <option value="breeds">Breeds of dogs</option>
                </select>
                <br />
        
                <input type="submit" value="click me" />
        </form>


<?php

echo "</body>";
echo "</html>";

}


else // GET returns names of pets 

{

        switch($_POST["category"])
        {
        
        case "names":       
                $data = array("nacho", "tippy", "cocoa");
                break;
        
                
        case "breeds":
                $data = array("corgi", "boxer", "poodle");
                break;

        default:
                $data = array("error");
                break;

        }


        header("Content-Type: application/json");
        echo json_encode($data);

}

/*
else
{
        echo "Hmm, something horrible happened.  I expected a POST.";
} 
*/      

?>

This–quite useful!–service lets you pick breeds of dogs I have owned, or names of dogs I have owned. Because, clearly, that is what everyone wants to know. The results are returned as json and something does get produced, which is fantastic!!

Tomorrow I’ll start the “real work” of architecting this thing…

Tuesday Update: Hello, world.

Leave a comment