/Retrieve.php

https://gitlab.com/sutrix.hoa.tran/Research-Prestashop · PHP · 101 lines · 56 code · 7 blank · 38 comment · 11 complexity · 636a3a5c6340838b043b43bdd2414248 MD5 · raw file

  1. <html><head><title>CRUD Tutorial - Retrieve example</title></head><body>
  2. <?php
  3. /*
  4. * 2007-2013 PrestaShop
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to license@prestashop.com so we can send you a copy immediately.
  15. *
  16. * DISCLAIMER
  17. *
  18. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  19. * versions in the future. If you wish to customize PrestaShop for your
  20. * needs please refer to http://www.prestashop.com for more information.
  21. *
  22. * @author PrestaShop SA <contact@prestashop.com>
  23. * @copyright 2007-2013 PrestaShop SA
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. * PrestaShop Webservice Library
  27. * @package PrestaShopWebservice
  28. */
  29. // Here we define constants /!\ You need to replace this parameters
  30. define('DEBUG', true); // Debug mode
  31. define('PS_SHOP_PATH', 'http://prestashop-research.local'); // Root path of your PrestaShop store
  32. define('PS_WS_AUTH_KEY', 'L77TLJE2RPDE6UYP4648R873RVWE961D'); // Auth key (Get it in your Back Office)
  33. require_once('./PSWebServiceLibrary.php');
  34. // Here we make the WebService Call
  35. try
  36. {
  37. $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
  38. // Here we set the option array for the Webservice : we want customers resources
  39. $opt['resource'] = 'customers';
  40. // We set an id if we want to retrieve infos from a customer
  41. if (isset($_GET['id']))
  42. $opt['id'] = (int)$_GET['id']; // cast string => int for security measures
  43. // Call
  44. $xml = $webService->get($opt);
  45. // Here we get the elements from children of customer markup which is children of prestashop root markup
  46. $resources = $xml->children()->children();
  47. }
  48. catch (PrestaShopWebserviceException $e)
  49. {
  50. // Here we are dealing with errors
  51. $trace = $e->getTrace();
  52. if ($trace[0]['args'][0] == 404) echo 'Bad ID';
  53. else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
  54. else echo 'Other error<br />'.$e->getMessage();
  55. }
  56. // We set the Title
  57. echo '<h1>Customers ';
  58. if (isset($_GET['id']))
  59. echo 'Details';
  60. else
  61. echo 'List';
  62. echo '</h1>';
  63. // We set a link to go back to list if we are in customer's details
  64. if (isset($_GET['id']))
  65. echo '<a href="?">Return to the list</a>';
  66. echo '<table border="5">';
  67. // if $resources is set we can lists element in it otherwise do nothing cause there's an error
  68. if (isset($resources))
  69. {
  70. if (!isset($_GET['id']))
  71. {
  72. echo '<tr><th>Id</th><th>More</th></tr>';
  73. foreach ($resources as $resource)
  74. {
  75. // Iterates on the found IDs
  76. echo '<tr><td>'.$resource->attributes().'</td><td>'.
  77. '<a href="?id='.$resource->attributes().'">Retrieve</a>'.
  78. '</td></tr>';
  79. }
  80. }
  81. else
  82. {
  83. foreach ($resources as $key => $resource)
  84. {
  85. // Iterates on customer's properties
  86. echo '<tr>';
  87. echo '<th>'.$key.'</th><td>'.$resource.'</td>';
  88. echo '</tr>';
  89. }
  90. }
  91. }
  92. echo '</table>';
  93. ?>
  94. </body></html>