• We just launched and are currently in beta. Join us as we build and grow the community.

Create a PHP API and Visual Basic App to Use It

randybryan845

Manga Panel Translator
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
81
Likes
40
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. }

    else

    {

  5. }
  6. ?>

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. ?>

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. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  4. Dim

    sendText As

    String
  5. If

    (TextBox1.Text.Length > 0) Then
  6. If

    (TextBox1.Text.Contains(" "

    )) Then
  7. For

    Each

    c As

    Char In

    TextBox1.Text
  8. If

    (c = " "

    Or

    c = Nothing

    ) Then
  9. sendText &= "%20"
  10. Else
  11. sendText &= c
  12. End

    If
  13. Next
  14. Else
  15. sendText = TextBox1.Text
  16. End

    If
  17. Else

    : sendText = "Hi%20There"
  18. End

    If
  19. Dim

    r As

    HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?write="

    & sendText)
  20. Dim

    re As

    HttpWebResponse = r.GetResponse()
  21. MsgBox("Request Finished"

    )
  22. End

    Sub

  23. Private

    Sub

    Button2_Click(sender As

    Object

    , e As

    EventArgs) Handles Button2.Click
  24. Dim

    r As

    HttpWebRequest = HttpWebRequest.Create("http://127.0.0.1/freelancerPHPAPI/index.php?check"

    )
  25. Dim

    re As

    HttpWebResponse = r.GetResponse()
  26. Dim

    src As

    String

    = New

    System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
  27. If

    (src.Contains("File written"

    )) Then
  28. MsgBox("File Already Written"

    )
  29. Else

    : MsgBox("File Not Yet Written!"

    )
  30. End

    If
  31. End

    Sub
  32. End

    Class


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top