php API in sketchware part 1

Apis are very beautiful way to send and recieve data from your server. api use MySql as server which allows us freedom to edit our data in our way.



Why use APis
As there is existence of firebase realtime databse, Api are not commonly used by small developers. There are some reasons why APIs Should be used by you instead of firebase realtime databse.

1. Firebase is free but limited. it is good for small developers but when it comes to large developers firebase limitations create so much problem.

2. Firebase offers only 100 active connections at a time. So if there are more than 100 active users then your app will show no response.

3. Firebase owns your data. That means firebase can use your data for it's product improvement.

4. Free firebase hosting gives 2gb of storage space .

So the outcome is that firebase is good for small developers but not good for developers with more than 100 active users or users creating content that need high storage.

So i am starting a series of youtube videos and blog posts on the topic How to make php API in sketchware.  


In first part i am telling you how to connect your app to your server.


Follow these steps to connect your android app to your server.


View area


In view area i added a button to send network request and a textview to show response. 

Server coding
For server i am currently using localhost provided by ksweb app. 


This app provides same functionality as xampp server in windows. After final coding i'll upload all this code to a live hosting server.

PHP Code
Here is the php code i have used 

Create a new database named sketch

<?php
//These are values for our databse
//Currently io am using localhost as database
//If you use any hosting then these values may change
//You can use ksweb app to test your api in your phone locally
//All links in description

$host = "localhost";
$user = "root";
$pass = "";
$db = "sketch";


//To connect to server with given keys
$con = mysqli_connect($host,$user,$pass, $db);

if($con){
//If connected then output will be
echo "Connected";
}else{
//If not connected then output will be
echo "no connect";
}

 ?>

Here you've to change values of host, user, pass and db according to your requirements. 
If you not set these values then this script will show errors



Save it as conn.php in your htdocs folder of your hosting or if you're using localhost then in /htdocs/ folder of your phone. 



Event area
In button onclick i used a request network component. 


And url will be 
yoursite.com/conn.php
In my case url is localhost:8000/conn.php

In Request network response i added textview set text to response. 

Turn on your localhost server and run your app.


Download project


Download ksweb apk
Previous Post Next Post