Opencart Controller Function to download file from RAPNET Diamond Trading Network

Following code will help Opencart Developers who need to use the RAPNET Diamond Trading Network for diamond trading website

I used the following approach to download and feed the data in db

Created a menu in admin which contain the download file option, then called ajax request to fire the request to the rapnet network asynchronously.

After these two steps called the ranpet file reading function to parse the file and insert the data in db for product master, product_to_description,product_to_category  and product_to_store

Other steps will discuss in upcoming posts and lets concentrate on the actual code which download the rapnet diamond data

To proceed with this you need a rapnet login and password as this is closed network and not free to all.

Once you logged into rapnet site under account menu you will find the menu to download the listing. Under that menu you can get the download url with various options

private function download_rapnet_file() {   set_time_limit(0); // to prevent the timeout error
 $feed_url =urldecode($this->model_catalog_rapnet->getConfig('feed_url'));// I created a model function to get this 
 $feed_url=str_replace('&','&',$feed_url); // replace all & occurances
$auth_url = $this->model_catalog_rapnet->getConfig('auth_url'); // another model function to get the auth url for rapnet
$username=$this->model_catalog_rapnet->getConfig('username'); // function to get the rapnet username 
$password=urlencode($this->model_catalog_rapnet->getConfig('pass'));// function to get the rapnet password
$feed_filename='import/rapnet.csv'; // file name where you want to save the downloaded csv file, you may append the timestamp to avoid overwrite
 $post_string = "username=".$username."&password=".$password;
 $request = curl_init($auth_url); // initiate curl object
 curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
 curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
 curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
 curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
 $auth_ticket = curl_exec($request); // execute curl post and store results in $auth_ticket
 curl_close ($request); $feed_url .= "&ticket=".$auth_ticket; //add authentication ticket:
 $fp = fopen($feed_filename, 'wb');
 if ($fp == FALSE){ echo "Unable to open the file"; exit; }
 $request = curl_init($feed_url); // initiate curl object
 curl_setopt($request, CURLOPT_FILE, $fp); //Ask cURL to write the contents to a file
 curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
 curl_setopt($request, CURLOPT_TIMEOUT, 1000); //set timeout to 5 mins
 curl_exec($request); // execute curl post
 curl_close ($request); // close curl object
 fclose($fp); //close file;  

The above controller function you can integrate in your custom controller or the default product controller in the opencart.

As the opencart provides the inbuilt error logging functionality so its not needed to use the try-catch block and other overheads to save errors .

The code is straight forward if someone need in deep understanding then do comment on the post


Comparison between Opencart And Magento

For last several months i have been working on two most used e commerce frameworks in open source Opencart and Magento

There are lot of comparisons are available between two saying which one is strong and flexible about support and other factors.

I would like to add some thing on this topic too

First I came across opencart framework around year back, at that time I was new to any framework, so started exploring and got  a totally new experience.

within a couple of mins i m ready with default framework options with some sample data a full working admin a nice template and working e commerce site.

After couple of days I showed it to my seniors and they asked me to prepare a demo for diamond trading  site. I have no reference in hand just a basic three page design.

home page category page and details page.

I searched for some content on web for diamond related thing and came up with

On this we got couple of more projects to do, and i become familiar with opencart. Such a simple framework, you dont have to do much to explore, and also if u need to change some functionality

the code is open and easy to modify

Few days later for some project the client is specifically asked for magento, another e commerce framework.

I downloaded next day and you dont believe it took around two days to understand the installation process and set up the default functionality

After week too i m struggling to set up the common options like category products reviews shopping cart etc

Its too horrible, another thing what i came across it not accepting localhost as the default host name, you need to provide the local IP address in the config file for admin to work

An explanation given by experts that its for live domain and for localhost

Another huge issue i got with database shifting from local to live, It took me around one hour to check where is damn config file for database

where in opencart its much simple in the main folder for admin and for front end u will get the config file with simple variables

Another issue with magento is bulk upload product say around 50k or 100k or more dynamically via script, It took me around two days to find out the exact database structure where magento insert the data

In couple of forums i got solution to use the magento default controller via Mage.php file and use the insert method with parameter assign and use it in loop

I thought the problem is solved but the script took horrible time to insert the products in bulk. I checked with fewer products it loaded fine but as the count increased the time increased exponentially

For opencart it took around 10 mins to understand the product database and i build the script in less than two hours for the first time and inserted the 100k records in less than two mins.

When it comes to customization of both the framework Opencart community always helped me a lot with quick response where in magento i got suggestions to use paid modules for the same functionality


Following image will explain you in detail characteristics of each of them

Comparision between opencart and magento

Comparision between opencart and magento