使用api prestashop获取所有类别和子类别

时间:2014-01-25 18:20:46

标签: php api prestashop

我对Prestashop WebService有一点问题。 可以帮助吗? 我想用prestashop API获取我店铺的所有类别和子类别。 我已经按照说明和阅读文档的例子,但我对使用“children()”和“attributes()”感到困惑。

这是我的代码:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_error', 1); 

define('DEBUG', false);                                         // Debug mode
define('PS_SHOP_PATH', 'http://localhost/myshop');                          // Root path of your PrestaShop store
define('PS_WS_AUTH_KEY', '*********************************');  // Auth key (Get it in your Back Office)
require_once('api/PSWebServiceLibrary.php');

// Here we make the WebService Call
try
{
        $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
        // Here we set the option array for the Webservice : we want customers resources
        $opt['resource'] = 'categories';
        // We set an id if we want to retrieve infos from a customer
        if (isset($_GET['id']))
                $opt['id'] = (int)$_GET['id']; // cast string => int for security measures

        // Call
        $xml = $webService->get($opt);

        // Here we get the elements from children of customer markup which is children of prestashop root markup
        $resources = $xml->children()->children();
}
catch (PrestaShopWebserviceException $e)
{
        // Here we are dealing with errors
        $trace = $e->getTrace();
        if ($trace[0]['args'][0] == 404) echo 'Bad ID';
        else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
        else echo 'Other error';
}




?>

然后呢? 如何检索类别的名称和ID? 我如何检索子类别的名称,ID和分类?

提前致谢。

2 个答案:

答案 0 :(得分:2)

在此阶段,Prestashop会返回类别网址的集合。 然后,您必须在每个URL上再次调用Prestashop API以使用

获取实际的类别数据
$resources->id ; $resources->name->language[0][0];

您的网络浏览器是您的朋友。首先使用浏览器尝试任何命令,看看你得到了什么。 我希望它有所帮助。

答案 1 :(得分:1)

检查http://doc.prestashop.com/display/PS15/Chapter+8+-+Advanced+Use#Chapter8-AdvancedUse-RenderingOptions

中的渲染选项

您可能必须在$ opt中使用显示选项获取名称或除id之外的任何其他参数...