Wednesday, 11 July 2018

RestFul Web Services with PHP Mysqli

RestFul Web Services with PHP Mysqli. In my last post about web service i explain how to create web service using PHP and test your service using REST Client, but MySQL is deprecated in current version so make update and show how to create your web service in MySQli. Before start please read my last post about service to get idea how to user Advance REST client.

Build your first web service with PHP, JSON and MySql


Before start i hope you have good command on oops concept. Now lets get started.
In this application i show you how to perform CRUD operation on database  using PHP MySQLI.
Step 1. Create DB in your phpmyadmin. Now paste this code in query box.
Step 2. Now create connection file to connect with database
Now once your connection with database is created we are ready to perform CRUD operation.
Now in this post i explain you two different method.
1. SEND DATA IN NORMAL FORMAT LIKE $_POST[‘FNAME’] = ‘ANEH’;  $_POST[‘LNAME’] = ‘THAKUR’;.
2. SEND DATA IN JSON OBJECT LIKE $_POST[‘KEYS’] =  “{‘FNAME’:’ANEH’, ‘LNAME’:’THAKUR’}”;
Step 3. Send data in simple format.
3.1 Insert data to database. First include connection file in your insert.php or copy/paste below code.
Insert data
3.2. Retrieve data from database and send data in json webservice Create select.php and call this file from REST Client.
After click on submit button get you get all data from database in JSON format
3.3. Now create new file to perform delete operation. I named my file delete.php and paste bellow code.
Now add delete link to your rest client and pass parameter to perform delete operation.
3.4. Create new file to perform updae operation. I named my file update.php and paste bellow code to update data and get response.
update.php
3.5. A very new and good feature of MySQLI is to perform MultiQuery operation which improve speed and save no of line of code.create file multiquery.php and paste below code to perform operation.
Step 4. Now send data in JSONFormat and perform CURD operation. This is very simple and easy to use the only thing you need to know about JSON.
JSON, OR JAVASCRIPT OBJECT NOTATION, IS A MINIMAL, READABLE FORMAT FOR STRUCTURING DATA. IT IS USED PRIMARILY TO TRANSMIT DATA BETWEEN A SERVER AND WEB APPLICATION, AS AN ALTERNATIVE TO XML.
Now when you need to send data to server create a post variable and add your json data in it.
Now we bind above json data in $_POST variable and after you send your data to server you need to decode json data also we need stripslashes() to remove slashes from request.
Once you read data from json request save data in variable and remaining part are same.
jinsert

Hope this will help you in creating web service :-).

0 comments:

Post a Comment