help with using product controller

I’m trying to use curl to log in to admin, and then use the add mode of products.php to add new products from another php script. The other php page is a web service using nusoap that my .net backend will be updating cscart from our backend systems. Does anyone have experience with this at all? Is it necessary for me to login since the scripts are on the same server? Is there something special I need to do? Any information at all appreciated greatly.



Our old integration did everything itself directly with the database & files etc, but I wanted to make this new integration like an add on that would be compatible with updates and a larger spectrum of cscart configurations.



Also I don’t know how I am supposed to structure my webservice into the addons directory. All the addons seem geared to enhancing the look and feel of the website somehow rather than interacting with the underlying business logic to add/change/delete products/orders/categories/customers etc. Also the addons seem to rely on inserting themselve via hooks and pre/post controllers. My addon will manipulate the controllers, not be called by them.



Though I don’t think it would be good practice, Is it possible to include the product.php file into my php script and pass the product_data and images in as parameters?



Thanks,



Scott

I have been experimenting more with curl, but I still cannot get logged in for some reason. This is very frustrating isn’t there any documentation on this or anyone who has any experience with this? It seems like no one knows anything in the forums about this product, maybe I wasted my money on buying it.



function AddProduct($product_data, $FileBytes){

$filename = “/media/data/www/webservice/image_upload/$code.jpg”;

UploadFile($FileBytes,$filename);

// INIT CURL

$ch = curl_init(‘http://webserv01.gva-twn.com/cscart/admin.php’);

// ENABLE HTTP POST

curl_setopt ($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

curl_setopt($ch, CURLOPT_HEADER,0);

curl_setopt ($ch, CURLOPT_COOKIEJAR, ‘cookie.txt’);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_POSTFIELDS, $parameters);

//login to the admin section

$parameters= array(‘return_url’ => ‘admin.php’, ‘user_login’ => ‘root’, ‘password’ => ‘password’, ‘dispatch[auth.login]’ => ‘Sign in’);

$returndata1 = curl_exec ($ch);

$date = new DateTime();

$datef = $date->format( ‘mdYHis’);

file_put_contents(“returndata1_$datef.txt”, $returndata1, FILE_APPEND);

$httpCode1 = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$err1 = curl_error($ch);



curl_setopt($ch, CURLOPT_URL, ‘http://webserv01.gva-twn.com/cscart/admin.php?dispatch=products.add’);

//post the data to new products.php

$parameters = array(‘file_product_main_image_icon[0]’ => ‘@’.$filename);

foreach ($product_data as $key => $value) {

$parameters[‘product_data[’.$key.‘]’] = $value;

}

$returndata2 = curl_exec ($ch);

file_put_contents(“returndata2_$datef.txt”, $returndata1, FILE_APPEND);

$httpCode2 = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$err2 = curl_error($ch);

if ($httpCode1==0) {

return “error from first post $err1”;

}

elseif ($httpCode2==0) {

return “error from second post $err2”;

}

else {

return $returndata2;

}

I just wanted to report I have been able to:


  1. call a php nusoap webservice from VB.Net
  2. That webservice then uses curl to login to admin and post a new product including picture using the products.php mode=add



    So I have all the information necessary to build my web service completely now for cscart integration via web service!



    I’m very happy now. :slight_smile:

Hi Scottemick,



Can you share the way you did it? I also have RMS and I’m interested in how to update products from RMS through webservices and also download orders.



Regards,

Samhn

[quote name=‘scottemick’]I just wanted to report I have been able to:


  1. call a php nusoap webservice from VB.Net
  2. That webservice then uses curl to login to admin and post a new product including picture using the products.php mode=add



    So I have all the information necessary to build my web service completely now for cscart integration via web service!



    I’m very happy now. :)[/QUOTE]



    Why not just make a PHP webservice that would insert directly to MySQL where you would post the needed info?



    Curl is such a crude solution for this.

I did write a integration between Dynamics GP and cscart previously that accessed the GP Dynamics database directly and the cscart database directly, though not with a webservice. To use it, we had to have access to the mysql port on the web server.



For the 2nd generation product I wanted to use the api’s that were available with cscart and Great Plains because the cscart database structure has changed a few times, and I wanted to have the product be installed as an add-on. So I thought using the controllers would be the way to go. On the GP side, I have gotten rid of having to poll the database every 15 min and look for changes to update cscart. Instead I am now using econnect’s outgoing service to automatically generate message queue entries for any objects that are changed. So when i get an item in the outgoing queue, my code will call the webservice which calls the cscart/admin.php?dispatch=products.add, which in turn calls the products.php page with mode=add.



I had to use curl because in order to call a controller I had to be logged in it seemed. Do you know of a better way to access cscart’s controllers? I’m really trying to make this a long-term stable solution.



Thanks,



Scott

The “best way” is as you’re doing it.

  1. Login to the admin area of the cart with an appropriate login
  2. POST the appropriate data to a controller in the appropriate mode.
  3. Read/parse the results.



    Doing DB to DB is faster but is then sensitive to any underllying changes in the DB that happen in upgrades. Using controller/mode and POSTing the data provides you the greatest “insulation” for the future.



    Another way (though I don’t recommend it) is to add your addon to the list of “secure controllers”. This will allow you to access your addon without logging in. The addon is respponsible for ensure ing the integrity of the caller. Hence you could then access your addon via [url]http://yoursite.com/admin.php?dispatch=your_addon.mode[/url]. Of course you will have to remove the “if( !defined(‘AREA’) )” code and insert your own security logic.

Thanks that is a really great idea to save a step with logging in, that would cut the time in half. But optimally I really could get away with only one login per product-category-orders synchronization session because I have curl saving the cookie.



And though I don’t want to, because I am running out of time to put out my 2nd integration solution I may have to go with Texas Guy’s idea initially though because it is a still a struggle for me to understand the whole process though I am getting better.



Back to studying this again -

[URL]http://docs.cs-cart.com/common.php?dispatch=docs/view&node_name=controllers[/URL]