通过API创建Magento产品不起作用


Magento Product Create via API not working

我已经与Magento SOAP web服务斗争了一周,我不知道为什么我不能使用API创建产品。

下面是我的PHP代码:

$client = new SoapClient('http://mywebsie.com/wp/store/api/soap/?wsdl');
// If some stuff requires api authentification,
// then get a session token
$session = $client->login('apiuser', 'apikey');
// get attribute set
$attributeSets = $client->call($session, 'product_attribute_set.list');
$attributeSet = current($attributeSets);
    $newProductData = array(    
    'name'              => 'Test product',
    'websites'          => array(1), 
    'short_description' => 'This is the short desc',
    'description'       => 'This is the long desc',                  
    'price'             => 150.00,                
    'status'            => 1,
    'tax_class_id'      => 0,
    'visibility'        => 4                             
    );      

    try {
        // product creation
        $client->call($session, 'product.create', array('simple', $set['set_id'], $ItemNmbr, $newProductData));                                   
    }
    catch(SoapFault $e) 
    {
        $msg = "Error in inserting product with sku $ItemNmbr : ".$e->getMessage();
        echo $msg;      
    }

我收到以下错误:

Error in inserting product with sku ING-ACCS-00009 : Invalid data given. Details in error message.

我想你是从http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalogProduct.html.有一些错误。这里有一个固定版本:

$client = new SoapClient('http://mywebsie.com/wp/store/api/soap/?wsdl');
// If some stuff requires api authentification,
// then get a session token
$session = $client->login('apiuser', 'apikey');
$newProductData = array(
    'name' => 'Test product',
    'websites' => array(1),
    'short_description' => 'This is the short desc',
    'description' => 'This is the long desc',
    'price' => 150.00,
    'status' => 1,
    'tax_class_id' => 0,
    'url_key' => 'product-url-key',
    'url_path' => 'product-url-path',
    'visibility' => '4',
);
$sku = 'Some unique sku';
$storeView = 1;
$attributeSetId = 4; // you can get this id from admin area
$productType = 'simple';
try {
    // product creation
    $client->call($session, 'catalog_product.create', array($productType, $attributeSetId, $sku, $newProductData, $storeView));
} catch (SoapFault $e) {
    echo "Error in inserting product with sku $sku : " . $e->getMessage();
}

对于Soap v2,这对我有效。

$client = new SoapClient('http://localhost/index.php/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $client->login('youruser', 'yourpasswd');
$attributeSets = $client->catalogProductAttributeSetList($sessionId);
$attributeSet = current($attributeSets);
$sku = 'COD002';
//catalogProductCreate( sessionId, type, setId, sku, productData, storeView )
try {
    $result = $client->catalogProductCreate($sessionId, 'simple', $attributeSet->set_id, $sku, array(
        'categories' => array(2),
        'websites' => array(1),
        'name' => 'producto de prueba',
        'description' => 'Product description',
        'short_description' => 'Product short description',
        'weight' => '10',
        'status' => '1',
        'url_key' => 'product-url-key',
        'url_path' => 'product-url-path',
        'visibility' => '4',
        'price' => '100',
        'tax_class_id' => 1,
        'meta_title' => 'Product meta title',
        'meta_keyword' => 'Product meta keyword',
        'meta_description' => 'Product meta description',
        'stock_data' => array(
                'qty' => '49', 
                'is_in_stock' => 1
            )
    ),1);
    $result2 = $client->catalogProductList($sessionId);
    echo "<pre>";
    print_r($result2);
    echo "</pre>";
} catch (SoapFault $e) {
    echo "Error in inserting product with sku $sku : " . $e->getMessage();
}