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.
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
Following Extension link for Chrome
Let’s start to check how to create, test and use web service with example.
Here, I have created login.php which will check user is a register or not.
Step 1 This is request data in json format
Step 2 At PHP side we get data as
Step 3 Then You can apply
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.
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
$msgtype = "success";
$updcode = "1";
$msgcode = 'User login successfully';
$msgtype = "fail";
$updcode = "0";
$msgcode = 'Username and password does not match.';
$result['result'][$msgtype] = $updcode;
$result['result']['message'] = mysql_real_escape_string($msgcode);
$result['result']['message'] = 'Requested data is not found in PHP script.';
echo json_encode($result); //Generated Response
$sqltypecond = '';
$db_sql = "select email,password,username from user where email = '".$email."' AND password='".$vpassword."'";
$result = $obj->select($db_sql);
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.