热输入php函数
您好,我正在尝试显示icecat产品数据,我是一个完整的php新手,我已经找到了一个我认为应该完成这项工作的代码片段,它说它要求您为函数提供EAN产品编号 有人能给我一个例子说明这意味着什么,因为我不知道,这是一个EAN产品编号EAN/UPC代码:0885909481842 下面是我尝试使用的代码片段热输入php函数,php,xml,Php,Xml,您好,我正在尝试显示icecat产品数据,我是一个完整的php新手,我已经找到了一个我认为应该完成这项工作的代码片段,它说它要求您为函数提供EAN产品编号 有人能给我一个例子说明这意味着什么,因为我不知道,这是一个EAN产品编号EAN/UPC代码:0885909481842 下面是我尝试使用的代码片段 function getICEcatProductSpecs($ean, $drawdescription = 0, $drawpicture = 0) { // Username
function getICEcatProductSpecs($ean, $drawdescription = 0, $drawpicture = 0)
{
// Username and password for usage with ICEcat
$username = "Your ICEcat username goes here";
$password = "Your ICEcat password goes here";
// Return 0 and exit function if no EAN available
if($ean == null)
{
return 0;
}
// Get the product specifications in XML format
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode($username.":".$password)
)
));
$data = file_get_contents('http://data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc='.$ean.';lang=nl;output=productxml', false, $context);
$xml = new SimpleXMLElement($data);
// Create arrays of item elements from the XML feed
$productPicture = $xml->xpath("//Product");
$productDescription = $xml->xpath("//ProductDescription");
$categories = $xml->xpath("//CategoryFeatureGroup");
$spec_items = $xml->xpath("//ProductFeature");
//Draw product specifications table if any specs available for the product
if($spec_items != null)
{
$categoryList = array();
foreach($categories as $categoryitem) {
$catId = intval($categoryitem->attributes());
$titleXML = new SimpleXMLElement($categoryitem->asXML());
$title = $titleXML->xpath("//Name");
$catName = $title[0]->attributes();
//echo $catId . $catName['Value']. "<br />";
$categoryList[$catId] = $catName['Value'];
}
$specs = "<table class='productspecs'>";
$i = 0;
$drawnCategories = array();
foreach($spec_items as $item) {
$specValue = $item->attributes();
$titleXML = new SimpleXMLElement($item->asXML());
$title = $titleXML->xpath("//Name");
$specName = $title[0]->attributes();
$specCategoryId = intval($specValue['CategoryFeatureGroup_ID']);
if($specName['Value'] != "Source data-sheet")
{
$class = $i%2==0?"odd":"even";
$specs .= "<tr class='".$class."'>
<td>
<table>";
if(!in_array($specCategoryId, $drawnCategories))
{
$specs .= " <tr class='speccategory'>
<th><h3>".$categoryList[$specCategoryId]."</h3></th>
</tr>";
$drawnCategories[$i] = $specCategoryId;
}
$specs .= " <tr>
<th>".utf8_decode($specName['Value']).":</th>
</tr>
<tr>
<td>";
if($specValue['Presentation_Value'] == "Y")
{
$specs .= "Ja <img src='".SCRIPT_ROOT."images/check_green.png' alt='Ja' />";
}
else if($specValue['Presentation_Value'] == "N")
{
$specs .= "Nee <img src='".SCRIPT_ROOT."images/check_red.png' alt='Nee' />";
}
else
{
$specs .= str_replace('\n', '<br />', utf8_decode($specValue['Presentation_Value']));
}
$specs .= "</td>
</tr>
</table>
</td>
</tr>";
}
$i++;
}
$specs .= "</table>";
//Draw product description and link to manufacturer if available
if( $drawdescription != 0)
{
foreach($productDescription as $item) {
$productValues = $item->attributes();
if($productValues['URL'] != null)
{
$specs .= "<p id='manufacturerlink'><a href='".$productValues['URL']."'>Productinformation from manufacturer</a></p>";
}
if($productValues['LongDesc'] != null)
{
$description = utf8_decode(str_replace('\n', '', $productValues['LongDesc']));
$description = str_replace('<b>', '<strong>', $description);
$description = str_replace('<B>', '<strong>', $description);
$description = str_replace('</b>', '</strong>', $description);
$specs .= "<p id='manudescription'>".$description."</p>";
}
}
}
//Draw product picture if available
if( $drawdescription != 0)
{
foreach($productPicture as $item) {
$productValues = $item->attributes();
if($productValues['HighPic'] != null)
{
$specs .= "<div id='manuprodpic'><img src='".$productValues['HighPic']."' alt='' /></div>";
}
}
}
return $specs;
}
else
{
return 0;
}
}
函数getICEcatProductSpecs($ean、$drawdescription=0、$drawpicture=0)
{
//用于ICEcat的用户名和密码
$username=“此处显示您的ICEcat用户名”;
$password=“您的ICEcat密码在此处”;
//如果没有可用的EAN,则返回0并退出函数
如果($ean==null)
{
返回0;
}
//以XML格式获取产品规范
$context=stream\u context\u create(数组(
“http'=>数组(
'header'=>“Authorization:Basic”.base64_编码($username.:“$password)
)
));
$data=文件\u获取\u内容('http://data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc=“.$ean.”;lang=nl;output=productxml',false,$context);
$xml=新的simplexmlement($data);
//从XML提要创建项元素数组
$productPicture=$xml->xpath(“//产品”);
$productDescription=$xml->xpath(“//productDescription”);
$categories=$xml->xpath(//CategoryFeatureGroup”);
$spec_items=$xml->xpath(//ProductFeature);
//绘制产品规格表(如果产品有任何规格)
如果($spec_items!=null)
{
$categoryList=array();
foreach($categoryitem中的类别){
$catId=intval($categoryitem->attributes());
$titleXML=newsimplexmlement($categoryitem->asXML());
$title=$titleXML->xpath(//Name”);
$catName=$title[0]->attributes();
//echo$catId.$catName['Value']。“”; $categoryList[$catId]=$catName['Value']; } $specs=“”; $i=0; $drawnCategories=array(); foreach($spec_项目作为$item){ $specValue=$item->attributes(); $titleXML=newsimplexmlement($item->asXML()); $title=$titleXML->xpath(//Name”); $specName=$title[0]->attributes(); $specCategoryId=intval($specValue['CategoryFeatureGroup_ID']); 如果($specName['Value']!=“源数据表”) { $class=$i%2==0?“奇数”:“偶数”; $specs.=” "; if(!in_数组($specCategoryId,$drawnCategories)) { $specs.=” “$categoryList[$specCategoryId]” "; $drawnCategories[$i]=$specCategoryId; } $specs.=” “.utf8_解码($specName['Value'])”: "; 如果($specValue['Presentation\u Value']==“Y”) { $specs.=“Ja”; } 否则如果($specValue['Presentation\u Value']==“N”) { $specs.=“Nee”; } 其他的 { $specs.=str_replace('\n','
',utf8_decode($specValue['Presentation_Value']); } $specs.=” "; } $i++; } $specs.=”; //绘制产品说明并链接至制造商(如有) 如果($drawdescription!=0) { foreach($productDescription作为$item){ $productValues=$item->attributes(); 如果($productValues['URL']!=null) { $specs.=“”; } 如果($productValues['LongDesc']!=null) { $description=utf8_解码(str_替换('\n','$productValues['LongDesc']); $description=str_replace(“,”,$description); $description=str_replace(“,”,$description); $description=str_replace(“”,“,$description); $specs.=“
””$description.“
”; } } } //绘制产品图片(如有) 如果($drawdescription!=0) { foreach($productPicture作为$item){ $productValues=$item->attributes(); 如果($productValues['HighPic']!=null) { $specs.=”; } } } 返回$specs; } 其他的 { 返回0; } }请提前给我任何建议,Peter我的理解是,如果调用此函数,您应该将EAN参数传递给它,如下所示:
getICEcatProductSpecs("0885909481842", 0, 0);
对不起,这是离题的。我们在这里帮助解决编程问题。基本的“如何编写代码”内容超出了网站的范围-我们不是来教你如何阅读代码的。你需要自己做这件事。