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.
Opencart Company ? Read 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:
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 API development. Thanks 🙂
Leave a Comment