In this post, I am going to explain, how to call web service using PHP. Nowadays every website is using android or iOS application. To use web data to the application web services are used. We can simply say web services are the bridge to connect web data with the mobile application.
Earlier I have explained how to use Webservices in WordPress but not written about how to call using core PHP So, I thought it’s right the time to write one now.
Web Service means some data comes at PHP side as Request, We serve the request in PHP and did some manipulation with the database and send Response.
For Testing a Web service, We can use extensions provided in chrome which is known as Advanced Rest Client.You can install the addon and check your Webservices.Same addon available for Mozilla also.
Following Add-ons link for Mozilla
https://addons.mozilla.org/en-us/firefox/addon/restclient/
Following Extension link for Chrome
http://code.google.com/p/chrome-rest-client/
Download addon and install it in your browser.That’s it. You are ready to use web services.
Let’s start to check how to create, test and use web service with example.
First of all, Create one file and set configuration of the database and physical path and relative path setting in it.
Next, create mysql class file for insert,update,delete function which is unique functionality for insert,delete,update any table data.
Here, I have created login.php which will check user is a register or not.
Step 1 This is request data in json format
1 2 3 | webdata={"email":"test@creativedev.in","password":"test123"} |
Step 2 At PHP side we get data as
1 2 3 | $_POST['webdata']='{"email":"test@creativedev.in","password":"123456"}'; |
Step 3 Then You can apply
1 2 3 | json_decode($_POST['webdata']); |
Step 4 Apply data manipulation to check user is exist or not.
Step 5 Then You will get generated Response.
Here, I am going to explaining using login example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | if(isset($_POST['webdata']) && $_POST['webdata']!='') { $string = $_POST['webdata']; $data = json_decode($string); // decode json data $email = $data->email; $password = md5($data->password); $resultsetuser = processlogin($email,$password); //Data manipulation check user is exit or not if(count($resultsetuser)> 0) { $msgtype = "success"; $updcode = "1"; $msgcode = 'User login successfully'; }else { $msgtype = "fail"; $updcode = "0"; $msgcode = 'Username and password does not match.'; } $result['result'][$msgtype] = $updcode; $result['result']['message'] = mysql_real_escape_string($msgcode); echo json_encode($result); }else{ $result['result']['message'] = 'Requested data is not found in PHP script.'; header('Content-type: application/json'); echo json_encode($result); //Generated Response } function processlogin($email,$vpassword) { global $obj; $sqltypecond = ''; $db_sql = "select email,password,username from user where email = '".$email."' AND password='".$vpassword."'"; $result = $obj->select($db_sql); return $result; } |
Explanation
Here login.php file will check web data is set when data requested via POST. If get data, decode that data. Next check decoded data with process login function that data exists in the database or not.
If data found, return success status with message or failure if not found. You need to pass data after encoding it so use json_encode to encoding a data.
Isn’t very easy to use web services with PHP? If you know any other better method, please share.If you have any comments or suggestion about this post, I would really appreciate it.
Comments (2)