Introduction

Hello Everyone

Today I shall tell you how to make Web Services for mobile applications in PHP. So what is a web service? “This is a service which is made to connect two different languages, to make them work together by shearing data”. For example Java and PHP are two different languages and in most websites we need them together for performing our functions.

Before going on further details let’s do this thing.

Outlines

  • Database
  • Extension
  • Web Service Connection File
  • Web Service GET Method
  • Web Service POST Method
  • Web Service REQUIRE Method
  • Web Service FILE Saving
  • Precautions
  • Conclusion

Description

The above outlines will be our steps for making web services. Just follow my lead and take a closer look.

Database

We need to create a database for web services which should contain a table as per our demand. I explained how to make database in my previous lecture “PHP file upload become easier now“.

Extension

You needed an extension to check your web services inside the browser. I am using “Advance Rest Client“. You will see that how it’s work when I shall run the files.

Web Service Connection File

What do we have here? We have a connection file, which shows our connection with the database.

Description

As we all knows that we need a connection file to connect to the database. You see I took five variables “$servername” , “$username” , “$password” , “$db” and “$result”. They contains the values. The serername is “localhost“. The username is “root“. I remained the password field empty because I have not assigned password while installing my XMPP, if you have assigned it then you should give password and in the end the last variable db contains the name of the “database” in which the table is.

I have an additional variable “$result”, It contains My sql_connection. I passed three variables inside it. after this I called mysql_select method and passed two variables inside it.

Web Service GET Method

I shall be using “if” and “else if” statements to check my parameters, that they passed or failed. Below there are number of statements, take a closer look.

username

As you can see inside the if statement I used GET method and passed my username inside it. Then I wrote three conditions inside my logic portion.

First condition carries false, second contains code. I assigned it zero which means If the name is not added then it will show the message stored inside the second response.

email

Check the below else if statement now.

As you can see in the above code, I just changed the value. This time I am passing or you can say checking the condition weather the email is written or not. If it is not entered the code will show zero response ans false while the message will display the text.

password

The below GET method will check the password field.

Now all the conditions are checked, we only need to insert our query. It also contains an other important portion. I defined the variables which are used in all the above code.

query

Take a closer look below.

This is the insert query which contains my table name. The variables are assigned to the respected columns. Now we need to check the response of our query the same way we did earlier. If the response is true it will display the code “200” and the message as follow. else the same message will show.

 

Setting the return type

The above code shows the return type I wanted for my web services. Then I included my connection file with it. The variable response contains an empty array so that the array can be able to display init.

The below is the full code of GET method. Take a closer look. You can do copy of this file and paste it.

The important thing I echo the json_encode and passed response inside it because I wanted the response inside json.

Web Service POST Method

the below code also contains the if and else if statements, the only different this is that The method is POST of web service.

username

Below is the POST method of web service for the username.

email

The below code shows the POST method of web service for the email.

password

The POST method of password in the web service is shown below.

file

It is an important point for my this web service, if you want to upload a file we need a pre-defined method name FILES. Take a close look below.

In the above code of web service I took a variable named locate. This tells the code where to store the file after upload. Then I concatenated the location of the file withe the file and it’s name. Then I shall check the response and will show the message if it is not working.

Inside My else if statement I made another if statement. This statement contains a predefined method named “move_uploaded_file”. It contains my image a temp name to it and the location where it needs to be saved. This method is used to save the file. Then I just checked the responses of my this method.

Inside the else statement I defined the variables which are used inside the whole code. Take a look below.

query

The below code of web service contains my query which will insert the data into the database. This is the same as I defined for the GET method above. But the only difference is that it contains an addition field named image, which will take my file.

While all the other responses are shown, I defined them to you above. Take a closer look on the message statement of the else portion. I concatenated the message with the predefined function error to check my web service.

Whole code

Below is the whole code which will show you the web service of POST method.

The important thing I echo the json_encode and passed response inside it because I wanted the response inside json.

 

Web Service REQUEST Method

Now the below code of web services is showing the same things we used before except the method is now request.

username

The below code shows the response as we displayed earlier.

This above code will display message when the username field will be empty.

email

The below statement will display the message when the field will remain empty.

password

This below code of web services will be displayed if the password field remain empty.

query

The below query is used to insert data foe the web service.

In the above code I passed the result variable to check my query working or not. It will display message of successful or unsuccessful as per the condition.

The Whole Code

The below is the whole code of web services REQUEST method.

You can copy it and run it. just replace the names as compared to you database. Then run this file.

Web Service File Saving

The below file will show the data back to us from the database.

You can just copy the code and run inside the browser.

The Web Services Images

The below images view shows the testing of web services. Take a closer look below.

Main

This is the extension file that I told you to save. In the url we need to give the link of the file from the local machine.

Web Services

$_POST

The below image shows the post method of the web service.

Web Services

On clicking the files the below dialog will appear from where we chose the file.

Web Services

As you can see in the below image the file name and type appeared after selection.

Web Services

When we save the file or run it the below image will appear, as you can see the message of success.

Web Services

And the raw view of the same message.

Web Services

$_GET

The below images will show the GET method of web services. As you can see in the url. See the method of writing the url.

Web Services

As you can see the below message appeared.

Web Services

The same response in json below.

Web Services

$_REQUEST

I wrote the url of the request method. First I shall check it with the get then with the post.

Web Services

The below picture shows the request method with post.

Web Services

As the below out put is same for both of them.

Web Services

Precautions

Change or replace the names of the columns as per of your database. Follow the sequence of else if statement. Don’t forget to declare the variables.

Conclusion

So what we did today? We created the web services with the help of GET, POST and REQUEST method. We created the data base. Added the extension. Then we wrote the required we services and run that. If something is missing or you didn’t understand, contact us…