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.
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.
After creating the API Key, we have login into the Opencart API using the created API key. In this blog, 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.
We used curl to access the API. The code below will provide you the process of writing flat PHP code to access the API.
$url = "http://my-opencart.com/index.php?route=api/login";
$params = array(
); // this array can contain the any number of parameter you need to send
$parameters = implode('&', $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
If we run this code, then it will return a JSON object.
On incorrect API Key:
On correct API Key:
The token return from the login API is required in order to access other APIs. We will discuss more on Opencart APIs. Thanks 🙂