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:
http://www.yoursite.com/index.php?name=yorkiebar
<?php
if(isSet($_GET['write']))
{
}else{
}
?>
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.
<?php
if(isSet($_GET['write']))
{
$file = fopen("test.txt","w");
fwrite($file, $_GET['write']);
}else{
}
?>
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.
<?php
if(isSet($_GET['write']))
{
$file = fopen("test.txt","w");
fwrite($file, $_GET['write']);
}else{
echo 'File written.';
}else{
echo 'File not written.';
}
}
?>
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
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:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sendText As String
If (TextBox1.Text.Length > 0) Then
If (TextBox1.Text.Contains(" ")) Then
For Each c As Char In TextBox1.Text
If (c = " " Or c = Nothing) Then
sendText &= "%20"
Else
sendText &= c
End If
Next
Else
sendText = TextBox1.Text
End If
Else : sendText = "Hi%20There"
End If
Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?write=" & sendText)
Dim re As HttpWebResponse = r.GetResponse()
MsgBox("Request Finished")
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.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?check")
Dim re As HttpWebResponse = r.GetResponse()
Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
If (src.Contains("File written")) Then
MsgBox("File Already Written")
Else : MsgBox("File Not Yet Written!")
End If
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:
<?php
if(isSet($_GET['write']))
{
$file = fopen("test.txt","w");
fwrite($file, $_GET['write']);
}else{
echo 'File written.';
}else{
echo 'File not written.';
}
}
?>
Visual Basic API Program:
Imports System.Net
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sendText As String
If (TextBox1.Text.Length > 0) Then
If (TextBox1.Text.Contains(" ")) Then
For Each c As Char In TextBox1.Text
If (c = " " Or c = Nothing) Then
sendText &= "%20"
Else
sendText &= c
End If
Next
Else
sendText = TextBox1.Text
End If
Else : sendText = "Hi%20There"
End If
Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?write=" & sendText)
Dim re As HttpWebResponse = r.GetResponse()
MsgBox("Request Finished")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim r As HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?check")
Dim re As HttpWebResponse = r.GetResponse()
Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
If (src.Contains("File written")) Then
MsgBox("File Already Written")
Else : MsgBox("File Not Yet Written!")
End If
End Sub
End Class