Reading list Switch to dark mode

    How to use OpenCart’s Rest API?

    Updated 27 December 2023

    As OpenCart already provides the REST API so we need to learn how to use the API. This is very helpful in order to use a store’s functionality from different devices irrespective of technology.

    In order to start, we have to add a new API. For that, we have to visit System->Users->API from the admin panel.

    addapi
    After clicking the add button, we will get into a form where we have to provide API name and API Key. The API key can be generated automatically after clicking on generate button.

    apikey
    After creating the API Key, we have login into the OpenCart API using the created API key. Here, I will tell you about the login part. We will discuss over the other files in further posts.

    In the folder catalog->controller->api, we have several files containing APIs. We have to log-in first, so need to access the login.php file.

    Searching for an experienced
    Opencart Company ?
    Find out More

    We used curl to access the API. The code below will provide you the process of writing flat PHP code to access the API.

    <?php
    
    	$url = "http://my-opencart.com/index.php?route=api/login";
    	$params = array(
    		'key=IENkO5W3VN2vKCEXYO4L6g3zGxM6LC1WdrylZF86gWSjgrBn80jHrIFcysXVCkJeoX1q8YEKwdT7eyaTN8MHSca41kXDpMtf53nN6npVEOOtK6ZL3LEn5eMO4Zj4bPdb14c22iaQJEBB2Lkl0Ef2AzXfdtwhyykDRZXqmVbEHweGkif353u0vdthYSqQtbXZWWD3LceyDUuuI8YGPZ5eflIes8SjM48sCw3h1mZj8w6hGSI1o6TOpuFAs6xjGpkc'
    		); // this array can contain the any number of parameter you need to send
    
    	$parameters = implode('&', $params);
    
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    	curl_setopt($ch,CURLOPT_POST, $parameter_count);
    	curl_setopt($ch,CURLOPT_POSTFIELDS, $parameters);
    
    	//execute post
    	$result = curl_exec($ch);
    
    	//close connection
    	curl_close($ch);
    
    	print_r($result);
    ?>

    If we run this code, then it will return a JSON object.

    On incorrect API Key:

    incorrectkey

    On correct API Key:

    correctkey
    The token return from the login API is required in order to access other APIs. We will discuss more on OpenCart API development. Thanks 🙂

    . . .

    Leave a Comment

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


    11 comments

  • prasoon
    • Webkul Support
  • Ankush Shinde
    • Webkul Support
  • Jansanman
  • Kshitij Baluni
    • Ivan Kutas
  • Kshitij Baluni
  • Suraj Mandal
  • Biplab Rout
    • Vikhyat Sharma
  • Back to Top

    Message Sent!

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

    Back to Home