<?php
define('DEBUG', true);
define('_PS_DEBUG_SQL_', true);
define('PS_SHOP_PATH', 'http://domain.ru');
define('PS_WS_AUTH_KEY', '12334567891234567894566987445');
require_once ('../PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/products?schema=synopsis'));
$resources = $xml->children()->children();
$resources->id;
$resources->id_category_default = 2;
$resources->id_default_image = 1;
$resources->price = 100;
$resources->available_for_order = 1;
$node = dom_import_simplexml($resources->name->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata name"));
$resources->name->language[0][0] = "New product name";
$resources->name->language[0][0]['id'] = 1;
$resources->name->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->description->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata description"));
$resources->description->language[0][0] = "description";
$resources->description->language[0][0]['id'] = 1;
$resources->description->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->description_short->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata description_short"));
$resources->description_short->language[0][0] = "description_short";
$resources->description_short->language[0][0]['id'] = 1;
$resources->description_short->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->link_rewrite->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata link_rewrite"));
$resources->link_rewrite->language[0][0] = "link-rewrite";
$resources->link_rewrite->language[0][0]['id'] = 1;
$resources->link_rewrite->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$resources->associations->categories->addChild('category')->addChild('id', 3);
$resources->associations->categories->addChild('category')->addChild('id', 4);
try
{
$opt = array('resource' => 'products');
$opt['postXml'] = $xml->asXML();
$xml_request = $webService->add($opt);
}
catch (PrestaShopWebserviceException $ex)
{
echo '<b>Error : '.$ex->getMessage().'</b>';
$trace = $ex->getTrace();
//echo($trace);
}
$xml_response = $xml_request['response'];
$response = new SimpleXMLElement($xml_response);
$resources = $response->children()->children();
$stock_available_id = $resources->associations->stock_availables->stock_available[0]->id;
$id_created_product = $resources->id;
$url = "http://domain.ru/api/images/products/$id_created_product";
$image_path = '/path/image.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.$image_path.';type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);