|
PHP is a very popular language for accessing databases for Websites. It is only natural, therefore, that developers may wish to use Microsoft Silverlight to send data to a PHP script, perhaps for database addition, deletion or retrieval. You can use the C# WebClient class to connect to a PHP script for this purpose as follows:
// Test Variables for username and password.
string username="test";
string password = "mypassword";
// Set the Parameters for the PHP Script.
string parameters = String.Format("?username={0}&password={1}",username,password);
// Create a Web Client for contacting the PHP Script.
WebClient client = new WebClient();
// Call the PHP Script.
// Add the appropriate header.
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
// Add a callback handle for when the Save operation is completed.
client.UploadStringCompleted += new UploadStringCompletedEventHandler(LogonCompleted);
// Invoke the script and its parameters.
client.UploadStringAsync(new Uri("http://localhost/logonscript.php"+parameters,UriKind.Absolute),"POST");
In the above code we are calling a sample logon script called 'logonscript.php'. This dummy script takes a username and password as a parameter. In the sample code above, we first create variables for storing a username and password ('test' and 'mypassword') respectively. We then format these variables as parameters that the PHP script can read so they will appear to the PHP script as '?username=test&password=mypassword'.
We then create a Web Client, set a URL Encoded Header for this client and add an UploadStringCompletedEventHandler (as we are sending parameters to the script). We then invoke the UploadStringAsync method to call the script with its parameters. In our example we are using an absolute path (UriKind.Absolute) and we are posting data to the script as denoted by the "POST" parameter.
In the example the full path is 'http://localhost/logonscript.php?username=test&password=mypassword' where the dummy script name is 'logonscript.php'. This script would reside on a Web Server such as Apache or Microsoft IIS (Internet Information Services).
We referred to a callback handler above. Once the script has run this function is invoked to notify the Silverlight application that it is completed. An example implementation of this callback function is shown below:
private void LogonCompleted(Object sender, UploadStringCompletedEventArgs e)
{
// We assume here that the script prints out "Success" or "Failure".
if(e.Result.Equals("Success"))
{
MessageBox.Show("Logon Succeeded");
}
else if(e.Result.Equals("Failure"))
{
MessageBox.Show("Logon Failed");
}
}
In this sample function the PHP script prints "Success" if the logon succeeded or "Failure" if the logon failed. This print out is stored in the Result member of event arguments parameter 'e'. We then check for these strings and display an appropriate message box.
The above example is for sending data to a PHP script. However, we can also retrieve data from a PHP script by print out the results and reading from the Result member of the events argument parameter 'e' in our callback function. Alternatively, if we are not sending parameters to the PHP script, we can use WebClient's DownloadStringAsync() method, set up a DownloadStringCompleted event and define a callback function with a signature similar to the following:
private void DownloadCompleted(Object sender, DownloadStringCompletedEventArgs e)
|