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


What you think ? Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s