简体   繁体   English

Web服务未显示任何内容

[英]Web Service doesn't show anything

I am trying to show the name and id of all the products on my shop, but i can't get it to work. 我正在尝试显示商店中所有产品的名称和ID,但我无法使它正常工作。

This is my code: 这是我的代码:

<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>

As you can see I use the display option to select id and name in order to show them, but I don't know how to use the result and show it. 如您所见,我使用显示选项来选择ID和名称以显示它们,但是我不知道如何使用结果并显示它。

Hi if someone has the same problem, I resolved with the following code: 您好,如果有人遇到相同的问题,我使用以下代码解决了:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM