Insert product in magento through Soap API with media images
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
error_reporting(E_ALL);
ini_set('display_errors', 1);
//create soap object
$mage_url = 'http://cohodeals.com/index.php/api/soap?wsdl';
$mage_user = 'chandresh';
$mage_api_key = '12345678';
// Initialize the SOAP client
$soap = new SoapClient($mage_url);
// Login to Magento
$session_id = $soap->login($mage_user, $mage_api_key);
$productData = array(
'name' => $_REQUEST['name'], //'Test product'
'categories' => array($_REQUEST['cat']),
'websites' => array(1),
'short_description' => $_REQUEST['desc'],
'description' => $_REQUEST['desc'],
'price' => $_REQUEST['price'],
'status' => 1,
'tax_class_id' => 0,
'url_key' => $_REQUEST['url'],
'visibility' => '4',
'stock_data' => array(
'qty' => $_REQUEST['qty'],
'is_in_stock' => 1
)
);
$sku = $_REQUEST['sku'];
$storeView = 1;
$attributeSetId = 4; // you can get this id from admin area
$productType = 'simple';
try {
$product_exist = null;
try{
$product_exist = $soap->call($session_id, 'product.info', $sku);
}
catch(exception $e){
//nothing to do
}
if($product_exist==null or $product_exist==""){
$result = $soap->call($session_id, 'catalog_product.create', array($productType, $attributeSetId, $sku, $productData, $storeView));
/* Image Upload area start */
//Get the file
$url = $_REQUEST['image'];
$content = file_get_contents($url);
//Store in media.
$imageInfo = pathinfo($url);
$imageName = $imageInfo['basename'];
$fp = fopen("../media/import/".$imageName, "w");
fwrite($fp, $content);
fclose($fp);
//attach image with database
require_once '../app/Mage.php';
Mage::app();
$path = Mage::getBaseDir('media') . DS . 'import' . DS . $imageName;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = base64_encode($data);
$productId = $result;
$file = array(
'content' => $base64,
'mime' => 'image/jpeg'
);
$result = $soap->call(
$session_id,
'catalog_product_attribute_media.create',
array(
$productId,
array('file'=>$file, 'label'=>'Label', 'position'=>'1', 'types'=>array('thumbnail','small_image','image'), 'exclude'=>0)
)
);
/* Image Upload area end */
}
else {
//$result = $soap->call($session_id, 'catalog_product.delete', $sku);
$result = $soap->call($session_id, 'catalog_product.update', array($sku,$productData));
}
echo 'Product Uploaded Successfully';
} catch (SoapFault $e) {
echo "Error in inserting product with sku $sku : " . $e->getMessage();
}