superbzyku Posted October 15, 2012 Share Posted October 15, 2012 (edited) Hi all! I suspect that my problem is pretty trivial, but I'm still learning ... I'm trying to using webservice add a new category in this way: <?php define('DEBUG', true); define('_PS_DEBUG_SQL_', true); define('PS_SHOP_PATH', 'http://localhost/prestashop/'); define('PS_WS_AUTH_KEY', '4FMZ6W8582SM5L43YKT8SVISX1LBUN3W'); require_once ('./PSWebServiceLibrary.php'); $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/categories?schema=synopsis')); $resources = $xml -> children() -> children(); $resources -> name -> language[0][0] = 'Test'; $resources -> name -> link_rewrite[0][0] = 'Test'; $resources -> active = 1; $resources -> id_shop_default = 1; $resources -> is_root_category = 0; $opt = array('resource' => 'categories'); $opt['postXml'] = $xml -> asXML(); $xml = $webService -> add($opt); ?> XML SENT xml=<?xml version="1.0" encoding="UTF-8"?> <prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> <category> <id_parent format="isUnsignedInt"/> <level_depth format="isUnsignedInt"/> <nb_products_recursive not_filterable="true"/> <active required="true" format="isBool">1</active> <id_shop_default format="isUnsignedId">1</id_shop_default> <is_root_category format="isBool">0</is_root_category> <position/> <date_add format="isDate"/> <date_upd format="isDate"/> <name required="true" maxSize="64" format="isCatalogName"><language id="1" xlink:href="http://localhost/prestashop/api/languages/1" format="isUnsignedId">Test</language><language id="7" xlink:href="http://localhost/prestashop/api/languages/7" format="isUnsignedId"/><link_rewrite>Test</link_rewrite></name> <link_rewrite required="true" maxSize="64" format="isLinkRewrite"><language id="1" xlink:href="http://localhost/prestashop/api/languages/1" format="isUnsignedId"/><language id="7" xlink:href="http://localhost/prestashop/api/languages/7" format="isUnsignedId"/></link_rewrite> <description format="isString"><language id="1" xlink:href="http://localhost/prestashop/api/languages/1" format="isUnsignedId"/><language id="7" xlink:href="http://localhost/prestashop/api/languages/7" format="isUnsignedId"/></description> <meta_title maxSize="128" format="isGenericName"><language id="1" xlink:href="http://localhost/prestashop/api/languages/1" format="isUnsignedId"/><language id="7" xlink:href="http://localhost/prestashop/api/languages/7" format="isUnsignedId"/></meta_title> <meta_description maxSize="255" format="isGenericName"><language id="1" xlink:href="http://localhost/prestashop/api/languages/1" format="isUnsignedId"/><language id="7" xlink:href="http://localhost/prestashop/api/languages/7" format="isUnsignedId"/></meta_description> <meta_keywords maxSize="255" format="isGenericName"><language id="1" xlink:href="http://localhost/prestashop/api/languages/1" format="isUnsignedId"/><language id="7" xlink:href="http://localhost/prestashop/api/languages/7" format="isUnsignedId"/></meta_keywords> <associations> <categories node_type="category"> <category> <id/> </category> </categories> <products node_type="product"> <product> <id/> </product> </products> </associations> </category> </prestashop> But I only get: Fatal error: Uncaught exception 'PrestaShopWebserviceException' with message 'This call to PrestaShop Web Services failed and returned an HTTP status of 400. That means: Bad Request.<br />Internal error. To see this error please display the PHP errors.' in E:\xampp\htdocs\ui\presta_update\PSWebServiceLibrary.php:218 Stack trace: #0 E:\xampp\htdocs\ui\presta_update\PSWebServiceLibrary.php(244): PrestaShopWebservice->parseXML(Array) #1 E:\xampp\htdocs\ui\presta_update\3-Create.php(27): PrestaShopWebservice->add(Array) #2 {main} thrown in E:\xampp\htdocs\ui\presta_update\PSWebServiceLibrary.php on line 218 I really depend on your help ... Edited October 16, 2012 by superbzyku (see edit history) Link to comment Share on other sites More sharing options...
superbzyku Posted October 16, 2012 Author Share Posted October 16, 2012 Welcome back! I wrote a function to add your own category. Is perhaps not 100% correct but for me it is enough It may be useful to someone: <?php define('DEBUG', true); define('_PS_DEBUG_SQL_', true); define('PS_SHOP_PATH', 'http://localhost/prestashop/'); define('PS_WS_AUTH_KEY', '4FMZ6W8582SM5L43YKT8SVISX1LBUN3W'); require_once ('./PSWebServiceLibrary.php'); include_once ('core_config.php'); // my config for spec. func. $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); function PS_new_category($n_is_root_category, $n_id_parent, $n_id_parent, $n_active, $n_l_id, $n_name, $n_desc, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords) { global $webService; $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/categories?schema=blank')); $resources = $xml -> children() -> children(); unset($resources -> id); unset($resources -> position); unset($resources -> id_shop_default); unset($resources -> date_add); unset($resources -> date_upd); $resources -> active = $n_active; $resources -> id_parent = $n_id_parent; $resources -> id_parent['xlink:href'] = PS_SHOP_PATH . '/api/categories/' . $n_id_parent; $resources -> is_root_category = $n_is_root_category; $node = dom_import_simplexml($resources -> name -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; $resources -> name -> language[0][0]['id'] = $n_l_id; $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; $resources -> description -> language[0][0]['id'] = $n_l_id; $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_title -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; $resources -> meta_title -> language[0][0]['id'] = $n_l_id; $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_description)); $resources -> meta_description -> language[0][0] = $n_meta_description; $resources -> meta_description -> language[0][0]['id'] = $n_l_id; $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; try { $opt = array('resource' => 'categories'); $opt['postXml'] = $xml -> asXML(); $xml = $webService -> add($opt); } catch (PrestaShopWebserviceException $ex) { czarodziej_log("PS/SYNCHRONIZACJA KATEGORII: " . $e -> getMessage(), 1); // my log function } } // simple use $n_name = 'New category name'; $n_desc = 'New desc ...'; $n_link_rewrite = 'someone_rewrite'; $n_meta_title = 'meta-title'; $n_meta_description = 'meta desc'; $n_meta_keywords = 'some,one,keywords'; $n_id_parent = '34'; $n_active = '1'; $n_l_id = '1'; $n_is_root_category = '1'; // run PS_new_category($n_is_root_category, $n_id_parent, $n_id_parent, $n_active, $n_l_id, $n_name, $n_desc, $n_link_rewrite, $n_meta_title, $n_meta_description, $n_meta_keywords); ?> 4 Link to comment Share on other sites More sharing options...
silentRun Posted March 21, 2013 Share Posted March 21, 2013 Thank you very much for your help ! It works great ! Link to comment Share on other sites More sharing options...
spherica Posted April 8, 2013 Share Posted April 8, 2013 i'm searching a similar script also to add a new product... can you help me? have you the right code? Link to comment Share on other sites More sharing options...
silentRun Posted April 10, 2013 Share Posted April 10, 2013 i'm searching a similar script also to add a new product... can you help me? have you the right code? Hi there, what are you exactly looking for ? Link to comment Share on other sites More sharing options...
LuisPL Posted June 19, 2013 Share Posted June 19, 2013 (edited) Hi there, what are you exactly looking for ? Edited June 19, 2013 by LuisPL (see edit history) Link to comment Share on other sites More sharing options...
mattix19 Posted October 16, 2013 Share Posted October 16, 2013 this code works form me: $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); $xml = $webService -> get(array('url' => PS_SHOP_PATH . '/api/categories?schema=synopsis')); $resources = $xml -> children() -> children(); unset($resources ->id); unset($resources ->position); unset($resources ->date_add); unset($resources ->date_upd); //unset($resources ->id_parent); //if unset category will be root. If set it must have id existing parent category! unset($resources ->level_depth); unset($resources ->nb_products_recursive); $resources ->id_parent = '6'; $resources ->name ->language[0][0] = 'test'; $resources ->link_rewrite->language[0][0] = 'test'; $resources ->active = 1; $resources ->id_shop_default = 1; $resources ->is_root_category = 0; $opt = array('resource' => 'categories'); $opt['postXml'] = $xml -> asXML(); $xml = $webService -> add($opt); 1 Link to comment Share on other sites More sharing options...
nathanphan Posted April 18, 2014 Share Posted April 18, 2014 Can I use this with a specified ID for new category? I want to insert my own Id for the category Thanks Link to comment Share on other sites More sharing options...
francois kizaru Posted February 15, 2016 Share Posted February 15, 2016 Hello, I tried in talend to insert a new category in prestashop but i have have "400|HTTP 400 Bad Request" . id_parent : "397" id_shop_default : "1" is_root_category : "0" name[langage] : "test" id : "1" name[link_rewrite] : "test" id : "1" name[description] : "test" id : "1" generally, this kind of problem happens when we don't fill the right fields . The parent category 397 is already created. Thanks for your help. Link to comment Share on other sites More sharing options...
Recommended Posts