Here I am going to explain new article about how to call web service in WordPress.
Web services are useful to retrieve your contents from PHP to mobile devices.whenever you want to get data into mobile from PHP and your PHP is using WordPress for a web application, you also have to follow rules of WordPress and PHP.
Well, here I am going to write one basic web service in WordPress and web service is about to login
into the mobile app using WordPress username and password.
Must Read: GoTo statement in PHP
So, let’s understand code written below:
First of all, create one folder, let’s say “webservices
” into your root of WordPress or into your theme folder.
Next create one file say “login.php
” and place below code into it.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php /* * Login Template * used to call webservices */ ini_set("memory_limit","128M"); // To load wordpress functions ,include file wp-load.php which is in root. $base_dir = dirname(dirname(__FILE__)); require_once($base_dir."/wp-load.php"); //pass post data into array webdata if(isset($_POST['webdata']) && !empty($_POST['webdata'])) { $user_post_data = stripslashes($_POST['webdata']); // now decode json data to separate post content $user_decode_data = json_decode($user_post_data); if(!isset($user_decode_data->user_login) || $user_decode_data->user_login == '') { $results_array['sMessage'] = 'Enter Valid Username.'; $results_array['sStatus'] = '1';//1=fail echo json_encode($results_array); exit; } if(!isset($user_decode_data->user_pass) || $user_decode_data->user_pass == '') { $results_array['sMessage'] = 'Enter Valid Password.'; $results_array['sStatus'] = '1';//1=fail echo json_encode($results_array); exit; } $username = trim($user_decode_data->user_login); $password = trim($user_decode_data->user_pass); $user = get_user_by( 'login', $username ); if ( $user && wp_check_password( $password, $user->data->user_pass, $user->ID)){ $results_array['sMessage'] = 'Login Successfully.'; $results_array['sStatus'] = '0';//0=success }else{ $results_array['sMessage'] = 'Login Failed.'; $results_array['sStatus'] = '1'; //1=fail } $json_data = json_encode($results_array); if(isset($_GET['callback']) && $_GET['callback']!=''){ print $_GET['callback'] ."(".$json_data.")"; }else{ echo $json_data; } }else { $msg['sMessage']='No Data in post'; } ?> |
Here, when you pass data and call this URL from mobile app,return data with encoding as JSON and into an array web data because function decode variable web data and using it to check into WordPress that valid username and password entered by user.
In above code, I am passing status and message with callback because when you are calling from mobile, you need to use JSONP data type which require callback to get return data.
That’s it. Hope this is useful to write web services into WordPress. To check your raw web service, you can use advanced rest client extension in chrome.
Hope this helps someone else out.As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.