简体   繁体   中英

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.

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.

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>';
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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