Introduction
I was recently working on a project where my device needed to wake up and run some routines, I also needed to be sure that app did not run at certain times. I really didn’t want to sit around to read local log files on the device so I created a quick routine that would log the info I needed to my Apache server. A little PHP, a little C# and yee-haw we're there.
Background
You may want to understand PHP, or you could just write the backend in ASP.NET which would also be easy. I chose not to do a web service simply to stay away from SOAP overhead. That could be conceived as good or bad depending on your viewpoint, it was just my opinion and I mainly wanted to see if it was really simple to do.
Building the Code
To start with fire up VS.NET 2003 and create a new project:
I’ve called my app PHPApp for this tutorial. Select OK and you well be prompted with an option dialog for the exe for this app we’ll select PocketPC – Windows Application.
Select OK. And you will have a blank form in the designer.
Now Drop a button on the Form – listed below in the code as OK. Now select the Name property of the button and change it to OK.
The Form now Contains a Button:
Double Click the Button and add the code listed below for the button event as well as the static object and the Event for writing data to the PHP web page. I’ve put static text in the button event, but you could easily change it to anything you want.
Using the code
There are a number of ways to use this code. You could just call all of it on a event such as a button click, or you could setup a protected method such as I’ll describe here. I then setup a static variable to the project to hold the state which I pulled from and xml config file which I won’t get into in this article but will address in a following one.
Be sure to use:
- using System.Net;
- using System.IO;
- using System.Text;
//I used a static variable so that in my other methods I could simply
//do and if statement so I’m going to list it here.
//I’m going to setup the IsPHPEnable member variable to true to be sure it
//uses my method for logging.
using System.Net;
using System.IO;
using System.Text;
Namespace PHPApp
{
public class Form1: System.Windows.Forms.Form
{
private static bool IsPHPEnabled=false;
public Form1()
{
/*Ok, now that we have the variable setup, lets move on to setting up the
Method itself that we’ll use when we want to log info to the Web Page I call
it WritePHPInfo and it takes a string for the message
*/
private static void WritePHPInfo(string message)
{
ASCIIEncoding encoding=new ASCIIEncoding();
string postData=DateTime.Now.ToShortTimeString();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest;
// Prepare web request...
try
{
myRequest = (HttpWebRequest)WebRequest.Create
("http://ipaq/oos.php?touch=" + message + " at:" +
DateTime.Now.ToShortTimeString() );
//************note: I’m passing the call to ?touch, you’ll see
//below in the php where touch is used for input
myRequest.Timeout=15000;
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Flush();
newStream.Close();
}
catch(WebException ex)
{
//be sure you look in here I’m not using it for this example
}
finally
{
myRequest=null;
}
}
/* Now lets look at how we can now call it, I’ll create a sample function,
lets say that were going to call it from a button on click event, so just
place a button on your app and call it OK for it’s name, go to the
Action Page and double click tThe on click event
*/
Private void OK_click(Object sender, System.EventArgs e)
{
// Now lets fire off our method
WritePHPInfo(“Calling to Web method via PHP and HttpWebRequest”);
}
//**PHP PORTION LOCATED ON APACHE SERVER**
/*Ok, so you can run PHP on Windows or Linux as well as Apache. I’m not going
to get into how to configure PHP to work with Windows but be assured it works
just fine. HINT: be sure you have php.exe setup for php extensions in IIS.
I chose to use Php on my Linux RedHat 9 machine so my path will be associated
with that.
On my system Apache is setup to look at /var/www/html as it root location for
serving up pages. IIS would be under \InetPub\wwwroot\. I then proceeded to
create a quick PHP page.. Granted to be to hard on me with the PHP I just
threw it up and it has very limited Exception handlers. I call it
logwriter.php I also created a form so I could test it from the actual
HTML gui.
*/
<html>
<body>
A Simple Form
<br><br>
<form action="logwriter.php" method="post">
<input type="text" name="touch" size="10">
<br><br>
<input type="submit" name="submitbutton">
</form>
</body>
</html>
<?php
$filename ="/tmp/data.txt";
/ file in this directory
$myFile= fopen($filename,'a');
if(! $myFile)
{
print ("File could not be opened.");
exit;
}
$string = "*****\n".$_REQUEST['touch']."\n*********\n";
fputs($myFile, $string);
fclose($myFile);
?>
Points of Interest
This wasn’t overly complex and only took a few minutes to bang out and saved me a little time standing in front of the device. I hope you might be able to use it in your next project.