Posted By

Monofunk on 04/25/10


Tagged

product products EAN specifications ICEcat


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Monofunk


ICEcat product specifications drawer


 / Published in: PHP
 

URL: http://nl.linkedin.com/pub/maarten-van-spil/7/a59/b7b

Use this PHP function to draw a nice table with product specifications gathered from an XML feed from ICEcat (http://www.icecat.biz/). It requires that you feed the function with an EAN product number and that you have registered with ICEcat. Set your username and password accordingly in the variables. You can choose to draw the product description and picture by setting $drawdescription and $drawpicture to 1.

The output is fully valid XHTML 1.0 Strict and is easily stylable using CSS.

  1. function getICEcatProductSpecs($ean, $drawdescription = 0, $drawpicture = 0)
  2. {
  3. // Username and password for usage with ICEcat
  4. $username = "Your ICEcat username goes here";
  5. $password = "Your ICEcat password goes here";
  6.  
  7. // Return 0 and exit function if no EAN available
  8. if($ean == null)
  9. {
  10. return 0;
  11. }
  12.  
  13. // Get the product specifications in XML format
  14. 'http' => array(
  15. 'header' => "Authorization: Basic " . base64_encode($username.":".$password)
  16. )
  17. ));
  18. $data = file_get_contents('http://data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc='.$ean.';lang=nl;output=productxml', false, $context);
  19. $xml = new SimpleXMLElement($data);
  20.  
  21. // Create arrays of item elements from the XML feed
  22. $productPicture = $xml->xpath("//Product");
  23. $productDescription = $xml->xpath("//ProductDescription");
  24. $categories = $xml->xpath("//CategoryFeatureGroup");
  25. $spec_items = $xml->xpath("//ProductFeature");
  26.  
  27. //Draw product specifications table if any specs available for the product
  28. if($spec_items != null)
  29. {
  30. $categoryList = array();
  31. foreach($categories as $categoryitem) {
  32. $catId = intval($categoryitem->attributes());
  33. $titleXML = new SimpleXMLElement($categoryitem->asXML());
  34. $title = $titleXML->xpath("//Name");
  35. $catName = $title[0]->attributes();
  36. //echo $catId . $catName['Value']. "<br />";
  37. $categoryList[$catId] = $catName['Value'];
  38. }
  39.  
  40. $specs = "<table class='productspecs'>";
  41. $i = 0;
  42.  
  43. $drawnCategories = array();
  44.  
  45. foreach($spec_items as $item) {
  46. $specValue = $item->attributes();
  47. $titleXML = new SimpleXMLElement($item->asXML());
  48. $title = $titleXML->xpath("//Name");
  49. $specName = $title[0]->attributes();
  50. $specCategoryId = intval($specValue['CategoryFeatureGroup_ID']);
  51.  
  52. if($specName['Value'] != "Source data-sheet")
  53. {
  54. $class = $i%2==0?"odd":"even";
  55. $specs .= "<tr class='".$class."'>
  56. <td>
  57. <table>";
  58. if(!in_array($specCategoryId, $drawnCategories))
  59. {
  60. $specs .= " <tr class='speccategory'>
  61. <th><h3>".$categoryList[$specCategoryId]."</h3></th>
  62. </tr>";
  63. $drawnCategories[$i] = $specCategoryId;
  64. }
  65. $specs .= " <tr>
  66. <th>".utf8_decode($specName['Value']).":</th>
  67. </tr>
  68. <tr>
  69. <td>";
  70. if($specValue['Presentation_Value'] == "Y")
  71. {
  72. $specs .= "Ja <img src='".SCRIPT_ROOT."images/check_green.png' alt='Ja' />";
  73. }
  74. else if($specValue['Presentation_Value'] == "N")
  75. {
  76. $specs .= "Nee <img src='".SCRIPT_ROOT."images/check_red.png' alt='Nee' />";
  77. }
  78. else
  79. {
  80. $specs .= str_replace('\n', '<br />', utf8_decode($specValue['Presentation_Value']));
  81. }
  82. $specs .= "</td>
  83. </tr>
  84. </table>
  85. </td>
  86. </tr>";
  87. }
  88. $i++;
  89. }
  90. $specs .= "</table>";
  91.  
  92. //Draw product description and link to manufacturer if available
  93. if( $drawdescription != 0)
  94. {
  95. foreach($productDescription as $item) {
  96. $productValues = $item->attributes();
  97. if($productValues['URL'] != null)
  98. {
  99. $specs .= "<p id='manufacturerlink'><a href='".$productValues['URL']."'>Productinformation from manufacturer</a></p>";
  100. }
  101. if($productValues['LongDesc'] != null)
  102. {
  103. $description = utf8_decode(str_replace('\n', '', $productValues['LongDesc']));
  104. $description = str_replace('<b>', '<strong>', $description);
  105. $description = str_replace('<B>', '<strong>', $description);
  106. $description = str_replace('</b>', '</strong>', $description);
  107. $specs .= "<p id='manudescription'>".$description."</p>";
  108. }
  109. }
  110. }
  111.  
  112. //Draw product picture if available
  113. if( $drawdescription != 0)
  114. {
  115. foreach($productPicture as $item) {
  116. $productValues = $item->attributes();
  117. if($productValues['HighPic'] != null)
  118. {
  119. $specs .= "<div id='manuprodpic'><img src='".$productValues['HighPic']."' alt='' /></div>";
  120. }
  121. }
  122. }
  123. return $specs;
  124. }
  125. else
  126. {
  127. return 0;
  128. }
  129. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: dbassa on May 4, 2012

Thanks for this code, it is great!

You need to login to post a comment.