Reading list Switch to dark mode

    How To Add IP In Opencart’s Rest API

    Updated 9 May 2023

    In our previous blog, we learnt about the login process of OpenCart API’s . Today we will learn , how to add the IP address in the OpenCart API.

    The process is very simple , we just need to go to System->Users->API and then click on edit API and go to IP Addresses tab . Here you can add the IP .

    screenshot_1683262206105-4

    After adding the IP address , we just need to click save and IP address will be saved .

    Now the question comes that why we need to add the IP address in the OpenCart API development? The reason is that without adding the IP address , we can not login with the API whether we are providing the correct key or not .

    You can easily understood this by following code file :

    Searching for an experienced
    Opencart Company ?
    Find out More
    /**
     * Webkul Software.
     *
     * @category Webkul
     * @package api
     * @author Webkul
     * Webkul Software Private Limited (https://webkul.com)
     * @license https://store.webkul.com/license.html
     */
    <?php
    class ControllerApiLogin extends Controller {
    	public function index() {
    		$this->load->language('api/login');
    
    		$json = $api_info = array();
    
    		$this->load->model('account/api');
    
    		// Login with API Key
    		if (isset($this->request->post['username']) && isset($this->request->post['key'])) {
    			$api_info = $this->model_account_api->login($this->request->post['username'], $this->request->post['key']);
    		} elseif (isset($this->request->post['key'])) {
    			$api_info = $this->model_account_api->login('Default', $this->request->post['key']);
    		}
    
    		if ($api_info) {
    			// Check if IP is allowed
    			$ip_data = array();
    	
    			$results = $this->model_account_api->getApiIps($api_info['api_id']);
    	
    			foreach ($results as $result) {
    				$ip_data[] = trim($result['ip']);
    			}
    	
    			if (!in_array($this->request->server['REMOTE_ADDR'], $ip_data)) {
    				$json['error']['ip'] = sprintf($this->language->get('error_ip'), $this->request->server['REMOTE_ADDR']);
    			}				
    				
    			if (!$json) {
    				$json['success'] = $this->language->get('text_success');
    				
    				$session = new Session($this->config->get('session_engine'), $this->registry);
    				
    				$session->start();
    				
    				$this->model_account_api->addApiSession($api_info['api_id'], $session->getId(), $this->request->server['REMOTE_ADDR']);
    				
    				$session->data['api_id'] = $api_info['api_id'];
    				
    				// Create Token
    				$json['api_token'] = $session->getId();
    			} else {
    				$json['error']['key'] = $this->language->get('error_key');
    			}
    		}
    		
    		$this->response->addHeader('Content-Type: application/json');
    		$this->response->setOutput(json_encode($json));
    	}
    }   
    

    The main reason behind the scene is to increase the security. For example if any how, some one got the api key , then also he/she can not login with the api and your data is safe .

    So the all process is done for the security purpose and to remove the chances of the theft of data .

    If you need custom Opencart Development services then feel free to reach us and also explore our exclusive range of Opencart Extensions.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Harish Kadli
  • Harish Kadli
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home