Magento产品通过API创建无法正常工作

时间:2013-12-06 09:08:07

标签: php api magento soap

我一直在与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.

2 个答案:

答案 0 :(得分:1)

我认为您从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();
}

答案 1 :(得分:0)

对于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();
}