Create a PHP API and Visual Basic App to Use It

Introduction: This tutorial we will be creating a simple PHP API for a website. This is useful for if you have a website which you want users to be able to interact with in third party programs seamlessly. Our API will only write a file and check if it already exists, you can make yours write to a database etc. Steps of Creation: PHP API Step 1: First we are going to create our PHP API and once that is finished we will create a simple Visual Basic application to use the API. To start, you will either need a domain or a localhost software which can run PHP, I am using XAMPP. In case you don't know website creation or development, the main/first page which is automatically visited once someone goes to a website address is named "index" (.php, .html etc). So create a new directory in your localhost or website control panel and add a file called "index.php", or you can use your own API file - just use that API file name later instead of when I use index.php. Step 2: Ok, now we have our file, lets first make it check for a GET statement named "write". GET Statements in PHP are simple variables which are contained in the URL of a web address after a ? and are assigned a value after a =. In the example below, there is a GET variable called name with the value of Yorkiebar:
  1. http://www.yoursite.com/index.php?name=yorkiebar
  1. <?php
  2.         if(isSet($_GET['write']))
  3.         {
  4.        
  5.         }else{
  6.        
  7.         }
  8. ?>
Because our simple API is only going to have two options (write and check) we can check for "write" and use the else statement, if you have more you can use elseif statements to check for multiple GET (or POST) statements. Step 3: Now in our write exists block we can make a simple script to write a file. We are only going to be using a file named "test.txt", as an extension of this tutorial you can try to make the name of the file a setting from our API and Software using the API.
  1. <?php
  2.         if(isSet($_GET['write']))
  3.         {
  4.                 $file = fopen("test.txt","w");
  5.                 fwrite($file, $_GET['write']);
  6.                 fclose($file);
  7.         }else{
  8.        
  9.         }
  10. ?>
Step 4: That's out simple write script finished, now let's create our check script. As I said at the beginning of this tutorial, this is only a very basic API and as a result only has minor script but the principle is the exact same for larger and more complex APIs.
  1. <?php
  2.         if(isSet($_GET['write']))
  3.         {
  4.                 $file = fopen("test.txt","w");
  5.                 fwrite($file, $_GET['write']);
  6.                 fclose($file);
  7.         }else{
  8.                 if (file_exists("test.txt")){
  9.                         echo 'File written.';
  10.                 }else{
  11.                         echo 'File not written.';
  12.                 }
  13.         }
  14. ?>
So if the file does already exists named "test.txt" in our current direction we made our file output "File written." to the source, and if not it will output "File not written.". Visual Basic Application Before: This project will require the Import of System.Net to use HTTPWebRequest and HTTPWebResponse
  1. Imports System.Net
Step 1: Now on to our Visual Basic Application. Create two buttons, one for writing and one for checking, then one textbox to contain the text to write to our file if the write button is clicked. Now, for the write button script we simple going to send a response to our website's API with ?write= followed by our textbox1.text - which is validated before hand:
  1.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.         Dim sendText As String
  3.         If (TextBox1.Text.Length > 0) Then
  4.             If (TextBox1.Text.Contains(" ")) Then
  5.                 For Each c As Char In TextBox1.Text
  6.                     If (c = " " Or c = Nothing) Then
  7.                         sendText &= "%20"
  8.                     Else
  9.                         sendText &= c
  10.                     End If
  11.                 Next
  12.             Else
  13.                 sendText = TextBox1.Text
  14.             End If
  15.         Else : sendText = "Hi%20There"
  16.         End If
  17.         Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?write=" & sendText)
  18.         Dim re As HttpWebResponse = r.GetResponse()
  19.         MsgBox("Request Finished")
  20.     End Sub
We remove the spaces from our textbox1.text with %20's just to make sure the text is written correctly since a web address can not contain spaces. As a note, if you are using your localhost too, you should also use 127.0.0.1 instead of "localhost". Step 2: Now, for our check script. This script just simply sends a request to the API and reads the response which we output in the API to the website text. If the website source contains "File written" it will output "File Already Written" and vice versa.
  1.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2.         Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?check")
  3.         Dim re As HttpWebResponse = r.GetResponse()
  4.         Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
  5.         If (src.Contains("File written")) Then
  6.             MsgBox("File Already Written")
  7.         Else : MsgBox("File Not Yet Written!")
  8.         End If
  9.     End Sub
Project Complete! There is our very basic PHP API and Visual Basic Application. Have a go at giving it some more functions. Below is the source and download to both the API and Visual Basic Solution Project: API:
  1. <?php
  2.         if(isSet($_GET['write']))
  3.         {
  4.                 $file = fopen("test.txt","w");
  5.                 fwrite($file, $_GET['write']);
  6.                 fclose($file);
  7.         }else{
  8.                 if (file_exists("test.txt")){
  9.                         echo 'File written.';
  10.                 }else{
  11.                         echo 'File not written.';
  12.                 }
  13.         }
  14. ?>
Visual Basic API Program:
  1. Imports System.Net
  2. Public Class Form1
  3.  
  4.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5.         Dim sendText As String
  6.         If (TextBox1.Text.Length > 0) Then
  7.             If (TextBox1.Text.Contains(" ")) Then
  8.                 For Each c As Char In TextBox1.Text
  9.                     If (c = " " Or c = Nothing) Then
  10.                         sendText &= "%20"
  11.                     Else
  12.                         sendText &= c
  13.                     End If
  14.                 Next
  15.             Else
  16.                 sendText = TextBox1.Text
  17.             End If
  18.         Else : sendText = "Hi%20There"
  19.         End If
  20.         Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?write=" & sendText)
  21.         Dim re As HttpWebResponse = r.GetResponse()
  22.         MsgBox("Request Finished")
  23.     End Sub
  24.  
  25.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  26.         Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?check")
  27.         Dim re As HttpWebResponse = r.GetResponse()
  28.         Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
  29.         If (src.Contains("File written")) Then
  30.             MsgBox("File Already Written")
  31.         Else : MsgBox("File Not Yet Written!")
  32.         End If
  33.     End Sub
  34. End Class

Comments

Add new comment