我正在尝试显示我店铺中所有产品的名称和ID,但我无法让它工作。
这是我的代码:
<html><head><title>Prueba CRUD - Lista Productos</title></head><body>
<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true); // Debug mode
define('PS_SHOP_PATH','http://192.168.1.124/prestashop'); // Root path of your PrestaShop store
define('PS_WS_AUTH_KEY','xxx'); // Auth key (Get it in your Back Office)
require_once('./PSWebServiceLibrary.php');
try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
// Here we set the option array for the Webservice : we want products resources
$opt = array(
'resource' => 'products',
'display' => '[id,name]'
);
// Call
$xml = $webService->get($opt);
// Here we get the elements from children of products
$resources = $xml->products->children();
}
catch (Exception $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 "Se ha producido un error: <br/>". $e->getMessage();
}
// We set the Title
echo "<h1>Lista Productos</h1>";
echo '<table border="5">';
// if $resources is set we can lists element in it otherwise do nothing cause there's an error
if (isset($resources))
{
echo '<tr><th>Id</th><th>Nombre</th></tr>';
foreach ($resources as $resource)
{
echo '<tr><td>'.$resource->attributes().'</td>
<td>'.$resource->attributes().'</td>
</tr>';
}
}
echo '</table>';
?>
</body></html>
如您所见,我使用display选项选择id和name以显示它们,但我不知道如何使用结果并显示它。
答案 0 :(得分:0)
如果有人遇到同样的问题,我解决了以下代码:
if (isset($resources))
{
echo '<tr><th>Id</th><th>Nombre</th></tr>';
foreach ($resources as $resource)
{
$probando = print_r($resource->name, true);
echo '<tr><td>'.$resource->id.'</td>
<td>'.$resource->name->language.'</td>
</tr>';
}
}