Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Create new product through SOAP API
 
Geraldton
Jr. Member
 
Total Posts:  4
Joined:  2008-07-22
 

I am tring to connect to Magento 1.1 SVN through Soap Api . I can browse products but when trying to create new product I get this error on the last line:

Fatal error: Uncaught SoapFault exception: [1] SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s) in D:\wamp\www\magento1.1\api.php:70 Stack trace: #0 [internal function]: SoapClient->__call(’call’, Array) #1 D:\wamp\www\magento1.1\api.php(70): SoapClient->call(’549oa13g7jmrt25...’, ‘product.create’, Array) #2 {main} thrown in D:\wamp\www\magento1.1\api.php on line 70

Here is the code I am using:

$client = new SoapClient('http://localhost/magento1.1/index.php/api/soap/?wsdl'); 

$session $client->login('apiUser''apiKey');
 
$products $client->call($session,'product.info','123'); 

//This works fine
var_dump($products); 

$attributeSets $client->call($session'product_attribute_set.list');
$set current($attributeSets);
$newProductSku='124';

$newProductData = array(
  
'sku'               => $newProductSku,
  
'name'              => 'name of product',
  
'websites'          => array('base'),
  
'short_description' => 'short description',
  
'description'       => 'description',
  
'price'             => 12.05
  
'status'            => 1
  
'category_ids'      => array(12), 
  
'weight'            => 3.5
  
'tax_class_id'      => 2
); 
 
$client->call($session'product.create', array('simple'$set['set_id']$newProductData));

Apperciate any help,
 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

I’m still trying to figure this API thing out myself (mostly because the documentation is extremely poor and in some cases [categories] incomplete).

I would try to put a try/catch block to catch the SoapFault exception and then display the message (Sometimes useful, not always).  Try to use this for all soap calls.
Like the following:

try {
$results 
$client->call($session'category.tree');
catch (SoapFault $e{
    
print $e->getMessage();
}

Also make sure you have the latest 1.1-trunk beta through SVN (which u might already).  And make sure you intialize the magento environment like so:

<?php

// initialize magento environment for 'default' store 
require_once('/***Your Magento Directory***/app/Mage.php'); 
Mage::app('default');

As for the $set[’set_id’] in product.create, make sure you understand what that is.  This is the ID to tell what attribute set the product belongs to, so by using that, you are creating a product in the first set off the setAttributes, which is not default, if that is what you wanted.  I found that default on my installation is “9”, but I have search function for it. So, in knowning this, there may or may not be certain attributes that are valid for each attribute set, which could cause the error (but I’m not sure).

I’m still trying to figure out how to assign categories (but your code might work for me).

 
Magento Community Magento Community
Magento Community
Magento Community
 
Geraldton
Jr. Member
 
Total Posts:  4
Joined:  2008-07-22
 

Thanks for reply, I do agree that API documentation is a bit overlooked specially in arguments part. I hope Magento team will spent more time on this part as it is the most important section for community specially developers to get invloved. I am a .Net developer and have some knowledge of PHP but I should admit I found Magento very interesting but confusing when it comes to modifications. We evaluated many eCommerce solutions and finally agreed to use Magento as our front side and develop the rest in .Net using SOAP API which makes it GO / NO GO fo us. I am also trying to communicate to Soap API using .Net but not much luck yet. If anyone has any experience on this or links to help will be apperciated.

Regarding your comments I am using latest 1.1 SVN version

I do use try catch block like the one below but it doesn’t give any more details than error message I have put. I did not paste all the code but I have :

//This is on top  
require_once 'app/Mage.php'
Mage::app('default'); 

// ....code posted above

try {
    
print "Product Create:";
  
$client->call($session'product.create', array('simple'$set['set_id']$newProductData));
catch (SoapFault $e{
    
echo "Product Create error::" var_dump($e);
}

my $set[’set_id’] returns 4 but since I have $set[’name’]=’Default’ I assume it is correct ( I do not have any other attribute set)

by the way using product.update as below works fine and I can modify my product with $newProductData but my issue is to create new one:

$client->call($session'product.update', array('<sku>'$newProductData));

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

I’m not sure what the problem is then.  I mean, the error seems to me that there is an uncaught exception.  The fact that it occurs on the last line might mean that there is some syntax error, unless that product create is at the end.

This is what I have so far to create a test product:  (it works, but I’m trying to figure out how to assign it to a category and website)

// Find and Set attribute set id for "Default"
$attributeSets $client->call($session'product_attribute_set.list');
$default_id 0;
foreach (
$attributeSets as $set{
    
if($set['name'== "Default"{
        $default_id 
$set['set_id'];
        break;
    
}
}


$newProductData 
= array(
    
'sku'               => 'sku_of_product',
    
'name'              => 'name of product',
    
'short_description' => 'short description',
    
'description'       => 'description',
    
'price'             => 12.05,
'status'            =>    1
);

try 
{
    $product_id 
$client->call($session'product.create', array('simple'$default_id$newProductData));
catch (SoapFault $e{
    
print $e->getMessage();  //Internal Error. Please see log for details.
    
echo "Product already created, no change occured.<br>";
}

$results 
$client->call($session'product.info''sku_of_product');

foreach (
$results as $key => $value{
    
print "$key $value<br>";
}

$client
->call($session'product.delete''sku_of_product');
try 
{
    
// Ensure that product deleted
    
var_dump($client->call($session'product.info''sku_of_product'));
catch (SoapFault $e{
    
print $e->getMessage();
    echo 
"Product already deleted";
}

Make sure you delete the product as well, or you will get a fault on creating the product twice.  Other than this, I can’t help.  I’m just starting to learn how to use an API like this.  My PHP programming skills are pretty rusty.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Geraldton
Jr. Member
 
Total Posts:  4
Joined:  2008-07-22
 

Thanks for reply, I tried your code which is basically very similar to mine but still the same error:

SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)

What version your are using? I am using latest SVN version on windows WAMP server with 209 tables but since you said this works for you I thought to give a try on (Beta 1.1 download) version which has 189 tables and it start working smile . I guess this is some sort of bug in the latest SVN version.

Thanks,

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

Sorry I couldnt help more.  I’m using Magento ver. 1.1.x.21 (the latest 1.1-trunk SVN release) on Windows XP, with Apache 2.2.6 and PHP 5.2.6. MYSQL 5.0.51a

 
Magento Community Magento Community
Magento Community
Magento Community
 
lakikeanu
Jr. Member
 
Total Posts:  24
Joined:  2008-06-16
 

To this code to work is there any settings needs to be done ?

$client = new SoapClient(’http://localhost/magento1.1/index.php/api/soap/?wsdl’);

there is no folder called API in my magento folder…
Need HELP ....

 
Magento Community Magento Community
Magento Community
Magento Community
 
lakikeanu
Jr. Member
 
Total Posts:  24
Joined:  2008-06-16
 

i tryied

require_once('app/Mage.php'); 

Mage::app('default');

$client = new SoapClient('http://localhost/magento/index.php/api/soap/?wsdl'); 

$session $client->login('test''test123');

$attributeSets $client->call($session'product_attribute_set.list');
$default_id 0;
foreach (
$attributeSets as $set{
    
if($set['name'== "Default"{
        $default_id 
$set['set_id'];
        break;
    
}
}


$newProductData 
= array(
    
'sku'               => 'sku_of_product',
    
'name'              => 'name of product',
    
'short_description' => 'short description',
    
'description'       => 'description',
    
'price'             => 12.05,
'status'            =>    1
);

try 
{
    $product_id 
$client->call($session'product.create', array('simple'$default_id$newProductData));
    
catch (SoapFault $e{
    
print $e->getMessage();  //Internal Error. Please see log for details.
    
echo "Product already created, no change occured.<br>";
}

it doesn’t show any error… but product is not inserted....
i use magento 1.1
please HELP....

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

Take a look at this thread for where I think API is (but I’m not certain):
http://www.magentocommerce.com/boards/viewthread/12367/P15/

I just figured out that they changed the “product.create” API for the new 1.1.1 release.

$newProductData = array(
    
'name'              => 'name of product',
    
'short_description' => 'short description',
    
'description'       => 'description',
    
'price'             => 12.05,
    
'status'            =>    1,
);

try 
{
    $product_id 
$client->call($session'product.create', array('simple'$default_id'sku_of_product'$newProductData));
catch (SoapFault $e{
    
print $e->getMessage();  //Internal Error. Please see log for details.
    
echo "Product already created, no change occured.<br>";
    exit();
}

Notice how they moved the “SKU” out of the last arguement and created a new arguement for it.  So it went from:

$product_id $client->call($session'product.create', array('simple'$default_id$newProductData));

to:

$product_id $client->call($session'product.create', array('simple'$default_id'sku_of_product'$newProductData));
 
Magento Community Magento Community
Magento Community
Magento Community
 
lakikeanu
Jr. Member
 
Total Posts:  24
Joined:  2008-06-16
 

for me

$client = new SoapClient(’http://localhost/magentostore/api/soap/?wsdl’);

line seems not working....

why is that ?
program doesn’t work after that line…

why ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
lakikeanu
Jr. Member
 
Total Posts:  24
Joined:  2008-06-16
 

$client = new SoapClient(’http://localhost/magento1.1/index.php/api/soap/?wsdl’);

to this code to run, do i need to install pear or do i need to do any settings changes ?

it doesnt work for me

 
Magento Community Magento Community
Magento Community
Magento Community
 
lakikeanu
Jr. Member
 
Total Posts:  24
Joined:  2008-06-16
 

i found that web service is NOT working in 1.1.1 STABLE version.... BUT it WORKS FINE in BETA version…

 
Magento Community Magento Community
Magento Community
Magento Community
 
avoigt3
Jr. Member
 
Total Posts:  4
Joined:  2008-07-11
 

The final version 1.1.1 has definitly bugs in the product API. The scripts i wrote on 1.1 BETA showing me the error “Fatal error: Uncaught exception ‘Zend_XmlRpc_Client_FaultException’ with message ‘SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)’ in /usr/share/php5/ZEND/Zend/XmlRpc/Client.php:286 Stack trace: ...”

 
Magento Community Magento Community
Magento Community
Magento Community
 
clorne
Sr. Member
 
Total Posts:  236
Joined:  2008-01-10
London
 

I am very sorry I do not know any of the answers to your API problems so this is just for your information

I downloaded the “FULL” .zip 1.1.1 version yesterday and set up a test site.

I have just copied the code for product update from the Magento API documentation

<?php
require_once('app/Mage.php'); 
Mage::app('default'); 
      
$proxy = new SoapClient('http://localhost/pecco/index.php/api/soap/?wsdl');

      
$sessionId $proxy->login('test''testkey');
 
$attributeSets $proxy->call($sessionId'product_attribute_set.list');

$set current($attributeSets);

$newProductData = array(
    
'name'              => 'xfred',
    
'websites'          => array('base'),
    
'short_description' => 'xxf',
    
'description'       => 'xfreddy',
    
'price'             => 12.05
);

// Create new product

$proxy->call($sessionId'product.create', array('simple'$set['set_id']'xxx'$newProductData)); 

// Get info of created product

var_dump($proxy->call($sessionId'product.info''xxx'));

?>

the var_dump result was as follows

array
‘product_id’ => string ‘2’ (length=1)
‘sku’ => string ‘xxx’ (length=3)
‘set’ => string ‘4’ (length=1)
‘type’ => string ‘simple’ (length=6)
‘categories’ =>
array
empty
‘websites’ =>
array
0 => string ‘0’ (length=1)
‘updated_at’ => string ‘2008-07-29 11:19:13’ (length=19)
‘created_at’ => string ‘2008-07-29 11:19:13’ (length=19)
‘type_id’ => string ‘simple’ (length=6)
‘name’ => string ‘xfred’ (length=5)
‘description’ => string ‘xfreddy’ (length=7)
‘short_description’ => string ‘xxf’ (length=3)
‘url_key’ => string ‘xfred’ (length=5)
‘visibility’ => string ‘4’ (length=1)
‘category_ids’ =>
array
empty
‘has_options’ => string ‘0’ (length=1)
‘gift_message_available’ => string ‘2’ (length=1)
‘price’ => string ‘12.0500’ (length=7)
‘tier_price’ =>
array
empty
‘options_container’ => string ‘container2’ (length=10)

Which is as I would have expected.

 
Magento Community Magento Community
Magento Community
Magento Community
 
avoigt3
Jr. Member
 
Total Posts:  4
Joined:  2008-07-11
 

Hi Clorne, your example shows the problem, the ‘sku’-parameter (your ‘xxx’) is in the final version not a member of the last array, it s now a single parameter. I’ve changed my script an the error is solved. Thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

@avoigt3 - FYI, that solution is exactly what I just posted above.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2