duanbohan2015 2013-10-14 11:47
浏览 58
已采纳

通过webservice prestashop添加产品后给出的参数错误

I have createed a simple add product to prestashop via webservice but when I try to add this to the DB I see only an error 'Bad parameters given'. What is wrong with my code? And how can I find if a category already exist with webservice?

  $reader = new XMLReader;
$reader->open($filename); // open an large xml with products and it should add this to db prestashop via webservice
$xml = $webService->get(array('url' => $shopUrl.'/api/products?schema=blank'));
$resources = $xml->children()->children();
foreach ($resources as $resource) {
  while ($reader->read()) {
    if($reader->nodeType == XMLReader::ELEMENT) { 
      $name = $reader->name;
    }

    if($reader->nodeType == XMLReader::TEXT || $reader->nodeType == XMLReader::CDATA){
        switch ($name) {
          case 'id':
           if ($reader->value != '0')
              $resource->id = $reader->value;
            break;

          case 'ilosc':
            $resource->quantity = $reader->value;
            break;

          case 'cena_netto':
            $resource->price = $reader->value;
            break;

          case 'opis':
            $resource->description = $reader->value;
            break;

          case 'waga':
            $resource->weight = $reader->value;
            break;

          case 'kod_kreskowy':
            $resource->ean13 = $reader->value;
            break;
        }
    }

    if($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'produkt'){
      $reader->next();
      $opt['postXml'] = $xml->asXML();
      $xml = $webService->add($opt);
    }
  }
}
  • 写回答

1条回答 默认 最新

  • dongsheng4126335 2014-08-22 07:04
    关注

    You may get the categories from categories?schema=blank, which will return you

    <?xml version="1.0" encoding="UTF-8"?>
    <prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <categories>
    <category id="1" xlink:href="http://www.yourshop/api/categories/1"/>
    <category id="2" xlink:href="http://www.yourshop/api/categories/2"/>
    <category id="3" xlink:href="http://www.yourshop/api/categories/3"/>
    </categories>
    </prestashop>
    

    Each of the individual xlink will return you:

    <?xml version="1.0" encoding="UTF-8"?>
    <prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <category>
        <id><![CDATA[2]]></id>
        <id_parent xlink:href="http://www.yourshop/api/categories/1"><![CDATA[1]]></id_parent>
        <level_depth><![CDATA[1]]></level_depth>
        <nb_products_recursive not_filterable="true"><![CDATA[169]]></nb_products_recursive>
        <active><![CDATA[1]]></active>
        <id_shop_default><![CDATA[1]]></id_shop_default>
        <is_root_category><![CDATA[1]]></is_root_category>
        <position><![CDATA[1]]></position>
        <date_add><![CDATA[2013-08-30 13:06:15]]></date_add>
        <date_upd><![CDATA[2013-08-30 13:06:15]]></date_upd>
        <name><language id="1" xlink:href="http://www.yourshop/api/languages/1"><![CDATA[Home]]></language><language id="2" xlink:href="http://www.yourshop/api/languages/2"><![CDATA[Home]]></language></name>
        <link_rewrite><language id="1" xlink:href="http://www.yourshop/api/languages/1"><![CDATA[home]]></language><language id="2" xlink:href="http://www.yourshop/api/languages/2"><![CDATA[home]]></language></link_rewrite>
        <description><language id="1" xlink:href="http://www.yourshop/api/languages/1"><![CDATA[]]></language><language id="2" xlink:href="http://www.yourshop/api/languages/2"><![CDATA[]]></language></description>
        <meta_title><language id="1" xlink:href="http://www.yourshop/api/languages/1"><![CDATA[]]></language><language id="2" xlink:href="http://www.yourshop/api/languages/2"><![CDATA[]]></language></meta_title>
        <meta_description><language id="1" xlink:href="http://www.yourshop/api/languages/1"><![CDATA[]]></language><language id="2" xlink:href="http://www.yourshop/api/languages/2"><![CDATA[]]></language></meta_description>
        <meta_keywords><language id="1" xlink:href="http://www.yourshop/api/languages/1"><![CDATA[]]></language><language id="2" xlink:href="http://www.yourshop/api/languages/2"><![CDATA[]]></language></meta_keywords>
    <associations>
    <categories node_type="category">
        <category xlink:href="http://www.yourshop/api/categories/3">
        <id><![CDATA[3]]></id>
        </category>
        <category xlink:href="http://www.yourshop/api/categories/4">
        <id><![CDATA[4]]></id>
        </category> 
        </category>
    </categories>
    <products node_type="product">
        <product xlink:href="http://www.yourshop/api/products/22">
        <id><![CDATA[22]]></id>
        </product>
        <product xlink:href="http://www.yourshop/api/products/24">
        <id><![CDATA[24]]></id> 
    </products>
    </associations>
    </category>
    </prestashop>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里