
  1. Code
  2. OpenCart

Create a Custom API in OpenCart


You'll need to create custom APIs for unique requirements in your project development at some point in time, and that's what we'll cover throughout the course of this tutorial. In our custom API module, we'll fetch the list of all products available in the store, and it'll be a JSON encoded output as required by the REST standards in OpenCart.

I assume that you're familiar with the basic module development process in OpenCart. Here's a nice article providing comprehensive insight into the subject just in case you would like to skip through it. Another important point: I'm using the latest version of OpenCart, that is as of writing this, and you should do that too to ensure the compatibility of core APIs.

Without wasting much of your time, I'll straight away dive into the practical stuff, and that's what the next section is all about.

A Glance at the File Setup

Let's have a look at the list of files required for the desired setup.

  • catalog/controller/api/custom.php: It's a controller file, and most of our application logic resides in this file.
  • catalog/language/en-gb/api/custom.php: It's a language file that holds language variables.
  • login.php: It's a file that demonstrates how to log in to the store using the REST API.
  • products.php: It's a file that demonstrates how to fetch products using our custom API module.

So, that's all it takes to set up our custom API module and test it using PHP CURL library.


Probably, it should be pretty familiar stuff if you're aware of the structure of OpenCart module files. However, we'll discuss the important snippets from the products method.

First of all, we have to check the authenticity of the request, and it's checked by the existence of the api_id variable in the active session. In the case of a valid and authenticated request, we'll go ahead and fetch all the products using the getProducts method of the core Product model. Of course, it'll give a permission denied error message in the case of invalid login.

Next, there's a generic security check to protect against CSRF attacks. It's accomplished by checking the existence of the HTTP_ORIGIN variable, and adding appropriate headers if it does exist.

Finally, we've used the json_encode function to encode the $products array, and the result is passed as an argument of the setOutput method.


So, that's it as far as the OpenCart-related file setup is concerned. From the next section onwards, we'll create the files that help us test our custom API using the PHP CURL library.

How It Works





The other important things to note are the CURLOPT_COOKIEJAR and  CURLOPT_COOKIEFILE settings. These set the file in which the cookies will be stored and read from. As we'll need to make authenticated calls, it's a must! Of course, you want to change the path /tmp/apicookie.txt according to your system settings. Make sure that it's writable by the web server too!

Finally, the function returns the response by the CURL request!

Obviously, the first thing to do is to start the session, and you'll need to use the login method. Let's have a look at an example. Go ahead and create a login.php file with the following contents.


Finally, we call the do_curl_request method to log in. Importantly, you should see a token variable in the $json object. Note down the value of that variable as we'll need to pass it while making subsequent API calls.

Next, let’s create a products.php file with the following contents.

The important snippet to note in the above example is the route querystring variable. It’s set to the api/custom/products value, which by convention calls the products method defined in the custom.php controller file created at the beginning of this tutorial. Also, we've passed the token variable along with its value to make sure that we have access to the API.






You could reach me via the feed section below, and I also spend a bit of time on Twitter if you prefer to contact me there!

Looking for something to help kick start your next project?