I have this function.
I am trying to convert in 2 functions. My objective is to have the best modularity and also extract a specific information like the dimension of the product, weight .... to include inside my DB.
1 - Display all information inside product page
2 - Extract specific information to include in DB (weight, depth, ...)
I don't know if it's the good way to do that.
it would be great if you can help me to resolve this element.
The original function
function icecat_to_array($data = array())
{
// Extract data array
extract($data);
if(isset($ean)){ $url = 'http://' . $username . ':' . $password . '@data.Icecat.biz/xml_s3/xml_server3.cgi?ean_upc=' . $ean . ';lang=' . $language . ';output=productxml'; }
// Get data
$xml = @file_get_contents($url,false,stream_context_create(array('http' => array('ignore_errors' => true))));
// Load into Simple XML Element
$xml = new SimpleXMLElement($xml);
$spec_group = $xml->xpath("/ICECAT-interface/Product/CategoryFeatureGroup");
$spec_item = $xml->xpath("/ICECAT-interface/Product/ProductFeature");
// Set specification groups
foreach($spec_group as $group)
{
$p['spec'][(int)$group[0]['ID']]['name'] = (string)$group->FeatureGroup->Name[0]['Value'];
}
// Set specifications
foreach($spec_item as $item)
{
if($item[0]['Value'] != 'Icecat.biz')
{
$p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['name'] = (string)$item->Feature->Name[0]['Value'];
$p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['value'] = (string)$item[0]['Value'];
$p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['sign'] = (string)$item->Feature->Measure->Signs->Sign;
$p['spec'][(int)$item[0]['CategoryFeatureGroup_ID']]['features'][(int)$item->Feature->Name[0]['ID']]['pres_value'] = (string)$item[0]['Presentation_Value'];
}
}
// Remove empty specification groups
foreach($p['spec'] as $key=>$value)
{
if(!isset($value['features'])){
unset($p['spec'][$key]);
}
}
}
// Get Icecat data in array by EAN number
$data = array(
'ean' => '4712900236903',
'language' => 'EN',
'username' => 'openIcecat-xml',
'password' => 'freeaccess'
);
$data = icecat_to_array($data);
echo '<pre>'.print_r($data,TRUE).'</pre>';
the result is :
[spec] => Array
(
[35] => Array
(
[name] => Processor
[features] => Array
(
[1036524] => Array
(
[name] => Processor frequency
[value] => 2.6
[sign] => GHz
[pres_value] => 2.6 GHz
)
[11271] => Array
(
[name] => Processor family
[value] => Intel Core i7-6xxx
[sign] =>
[pres_value] => 6th gen Intel® Core™ i7
)
.....
)
)
[108] => Array
(
[name] => Weight & dimensions
[features] => Array
(
[1525] => Array
(
[name] => Weight
[value] => 2591
[sign] => g
[pres_value] => 2.59 kg
)
[5143] => Array
(
[name] => Width
[value] => 384.5
[sign] => mm
[pres_value] => 384.5 mm
)
[5145] => Array
(
[name] => Depth
[value] => 256.9
[sign] => mm
[pres_value] => 256.9 mm
)
[117745] => Array
(
[name] => Height (front)
[value] => 32.75
[sign] => mm
[pres_value] => 3.27 cm
)
[117763] => Array
(
[name] => Height (rear)
[value] => 34.75
[sign] => mm
[pres_value] => 3.48 cm
)
)
)
...
)
My functions
// display the options category like Processor, Dimensions
public function getProductFeature11() {
$xml = $this->getSearchProductEanXML();
$category_feature_group = $xml->xpath("//CategoryFeatureGroup");
foreach($category_feature_group as $item) {
$test = new SimpleXMLElement($item->asXML());
$feature_group = $test->xpath("//FeatureGroup");
foreach($feature_group as $item1) {
var_dump($feature_group);
$test1 = new SimpleXMLElement($item1->asXML());
$name = $test1->Name['ID'] . ' ' . $test1->Name['Value'];
echo $name . '<br>';
}
}
}
result
1222500 Processor
1222498 Hard drive
4863 Memory
4865 Optical drive
4867 Display
4977 Audio
4995 Ports & interfaces
5023 Weight & dimensions
...
My second function
public function getProductFeature1() {
$xml = $this->getSearchProductEanXML();
$ProductFeature = $xml->xpath("//ProductFeature");
foreach($ProductFeature as $item2) {
$test2 = new SimpleXMLElement($item2->asXML());
$test2 = $item2->attributes();
echo '<br>' . $test2['CategoryFeature_ID'] . ' ' . $test2['Name'] . ' ' . $test2['Value'];
}
}
result
29776 Black
101037 Notebook
4692 Clamshell
59777 Gaming
2787 15.6
9350 1920 x 1080
36303 N
36493 Y
149038 Full HD
99902 Matt
16353 16:9
73715 2.6
....
after I don't know to merge this 2 functions if it's correct and to have the good element inside the good categories.
Thank you