Restful PHP web service Tutorial


Posted on 2015-07-14 11:21:59

Dear friends. 

About Restful web service in PHP, I will take guides from install extensions and create a new function, check result with rest standard server step by step. Step 1. You could download extension "Advanced REST client" on google chrome browser to check function. Pls, you can search and download, install at Capture Step 2. I will implement function login with restful client. Have 4 method popular for restful standard PHP language: Get, Post, Put, Delete Get: This method for function get information and return to mobile. Post: This method for function insert data to database or post data to server for implement action. Put: This method for main function update data to database. Delete: Implement action delete. Capture2   With action login, I used method Post with params: username, password. You must choose tab Form same picture. After  you input right input text, you can click "send" button. Capture3   You have seen result with status, msg, Token. Token will be used for other actions after you login success exp: change profile, get information user... Step 3. Code implement action login:  

function api_login() { if ($this->request->is('post')) { $username = $this->request->data['username']; $password = $this->request->data['password']; $user = $this->User->find('first', array('conditions' => array('username' => $username, 'password' => sha1($password), 'group' => GROUP_AGRONOMIST))); if (!empty($user)) { if ($user['User']['active']) { // Create login key $loginTime = date("Y-m-d H:i:s"); $loginKey = $user['User']['id'] . "key" . $loginTime; $loginKey = sha1($loginKey); // ---------------- // update login info $this->User->updateAll(array('User.login_last' => "'$loginTime'", 'User.login_key' => "'$loginKey'", 'User.login_count' => $user['User']['login_count'] + 1), array('' => $user['User']['id'])); // Write demo token //$this->Session->write('DemoToken',$loginKey); $this->api['status'] = API_STATUS_OK; $this->api['msg'] = $user['User']['id']; $this->api['token'] = $loginKey; } else { $this->api['status'] = API_STATUS_NG; $this->api['msg'] = __('MsgUserDisactive'); } } else { $this->api['status'] = API_STATUS_NG; $this->api['msg'] = __('MsgLoginFail'); } } }  

You can use its for run demo on your local. I hope this article help you for your problem. If you have problems you could write on comment, i will help you. You can see more guides about PHP at here