Skip to content
Advertisement

Securing php api to use in android application

I am newbie to android development. I am using android studio for developing an application. Things i have done

  1. Created a DB with two tables in it in MySQL.
  2. Created two separate api's for both GET and POST methods.
  3. Successfully accessed both api's

What i have achieved for now

  1. Able to GET data form the GET api.
  2. Able to POST data using the POST api

What i have to do now

I want my api to publish online, i.e. I want to deploy my services into a server and access them. At this point i am able to deploy the services on the server and accessed them.

Now i want my services(api's) to be secured. For that i have searched many articles and found two ways.

  1. Use yii framework. Someone told me to use it because it automatically secured the api's. But i don’t know for sure whether it do or not.
  2. Manually secure the api's

As for point 1 the framework will be helpful but it’s new to me and it will take time to coop with it as i am already created the web services.

For point 2 i got some information

  1. using HMAC_SHA1
  2. DETECTING MOBILE DEVICES USING PHP

Both links seems to be good but link 1 doesn’t gives me much info on that.

Obviously i want to secure my both api's

Now the code part

GET_DATA.php

JavaScript

echo json_encode(array(‘users’=>$result));

POST_DATA.php

JavaScript

I have a user class from which i am getting username and ID

JSONfunctions.java

This class is responsible for getting data from the api

JavaScript

PutUtility.Java

This class is responsible for POST method

JavaScript

MainActivity.java

JavaScript

As i am newbie, so i don’t know what to do in php script and what to do in my android code :(. It would be very helpful if anyone can guide me or give me a tutorial that i follow.

Any help would be highly appreciated.

Advertisement

Answer

To secure your APIs, you need a mechanism to detect that the request to the api is made from a trusted source. Many frameworks come with this feature by default.

However you can just use JSON Web Tokens (jwt) with PHP to add authorization to your api requests

Learn about token based authentication from this page.

Check out this simple tutorial on how to secure your PHP api endpoints with JWT.

If you need even more security you might want to add OAuth provider service to your API. check out this post on how to write OAuth provider in PHP

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement