[英]Web Service doesn't show anything
我正在嘗試顯示商店中所有產品的名稱和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>
如您所見,我使用顯示選項來選擇ID和名稱以顯示它們,但是我不知道如何使用結果並顯示它。
您好,如果有人遇到相同的問題,我使用以下代碼解決了:
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>';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.