我有这个功能。
我正在尝试转换2个函数。 我的目标是拥有最佳的模块化,并提取特定的信息,如产品的尺寸,重量....包括在我的数据库内。
1 - 显示产品页面内的所有信息
2 - 提取要包含在DB中的特定信息(重量,深度,......)
我不知道这是否是做到这一点的好方法。
如果你可以帮我解决这个问题,那将会很棒。
原始功能
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>';
结果是:
[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
)
)
)
...
)
我的功能
// 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>';
}
}
}
结果
1222500 Processor
1222498 Hard drive
4863 Memory
4865 Optical drive
4867 Display
4977 Audio
4995 Ports & interfaces
5023 Weight & dimensions
...
我的第二个功能
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'];
}
}
结果
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
...
我不知道合并这2个函数是否正确并且在良好类别中包含好元素。
谢谢
答案 0 :(得分:0)
function extract($user_data)
{
....
return $extracted_data;
}
function display($extracted_data)
{
....
return $results;
}
echo display(extract($user_input));
这样的东西?