我正在尝试使用Spreadshirt API来显示产品/文章。
我现在需要最终添加一个下拉列表来选择每个产品的尺寸,并添加一个按钮以使用spreadshirt添加到购物篮和结帐。
<?php
// This basket creation and checkout script shows you how to create a basket on the
// Spreadshirt platform using Spreadshirt API v1 and forward customers to the
// Spreadshirt checkout.
// 1. Get shop data
$shopUrl = "http://api.spreadshirt.net/api/v1/shops/665930";
$ch = curl_init($shopUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
$shop = new SimpleXMLElement($result);
$namespaces = $shop->getNamespaces(true);
// 2. Get article data
$attributes = $shop->articles->attributes($namespaces['xlink']);
$articlesUrl = $attributes->href;
$ch = curl_init($articlesUrl . "?fullData=true");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
$articles = new SimpleXMLElement($result);
$article = $articles->article[0];
$attributes = $article->attributes($namespaces['xlink']);
$articleUrl = $attributes->href;
$appearanceId = "".$article->product->appearance['id'];
$sizeId = null;
// 3. Get product type data
$attributes = $article->product->productType->attributes($namespaces['xlink']);
$productTypeUrl = $attributes->href;
$ch = curl_init($productTypeUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
// 4. Find valid size
$productType = new SimpleXMLElement($result);
foreach ($productType->stockStates->stockState as $stockState) {
if ($stockState->appearance['id'] == $appearanceId &&
$stockState->available == "true") {
$sizeId = $stockState->size['id'];
break;
}
}
// 5. Create basket
$basket = new SimpleXMLElement(getFileData("basket.xml"));
$basketItem = new SimpleXMLElement(getFileData("basketitem.xml"));
$itemAttributes = $basketItem->element->attributes($namespaces['xlink']);
$itemAttributes->href = $articleUrl;
$basketItem->element->properties->property[0] = $appearanceId;
$basketItem->element->properties->property[1] = $sizeId;
$attributes = $shop->baskets->attributes($namespaces['xlink']);
$basketsUrl = $attributes->href;
$header = array();
$header[] = createSprdAuthHeader("POST", $basketsUrl);
$header[] = "Content-Type: application/xml";
$ch = curl_init($basketsUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $basket->asXML());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
$basketUrl = parseHttpHeaders($result, "Location");
// 6. Create basket item
$basketItemsUrl = $basketUrl."/items";
$header = array();
$header[] = createSprdAuthHeader("POST", $basketItemsUrl);
$header[] = "Content-Type: application/xml";
$ch = curl_init($basketItemsUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $basketItem->asXML());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
// 7. Get checkout url
$basketCheckoutUrl = $basketUrl."/checkout";
$header = array();
$header[] = createSprdAuthHeader("GET", $basketCheckoutUrl);
$header[] = "Content-Type: application/xml";
$ch = curl_init($basketCheckoutUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
$checkoutRef = new SimpleXMLElement($result);
$refAttributes = $checkoutRef->attributes($namespaces['xlink']);
$checkoutUrl = $refAttributes->href;
echo '<html><body>';
foreach ($articles->article as $article) {
$resource = $article->resources->resource[0];
$attributes = $resource->attributes($namespaces['xlink']);
echo '<div style="clear:both;">';
echo '<div style="float: left; width: 150px; height: 150px;"><img src="' . $attributes->href . '?width=150&height=150" width="150" height="150"/></div>';
echo 'Product ID is:' . (int)$article->product->attributes()->id . '</br>';
echo 'Article ID is:' . (int)$article->attributes()->id . '</br>';
echo 'Article Price is:' . $article->price[0]->vatIncluded[0] . '</br>';
echo 'Article Name is:' . $article->name . '</br>';
echo 'Article Description is:' . $article->description . '</br>';
echo '<select id="size-select" name="size">';
foreach($productType->sizes->size as $val) {
echo '<option value="'.$val['id'].'">'.$val->name.'</option>' ;
}
echo '</select>';
echo '</div>';
}
echo '</body></html>';
function createSprdAuthHeader($method, $url) {
$apiKey = "API";
$secret = "SECRET";
$time = time()*1000;
$data = "$method $url $time";
$sig = sha1("$data $secret");
return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
}
function parseHttpHeaders( $header, $headername ) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/('.$headername.'): (.+)/m', $field, $match) ) {
return $match[2];
}
}
return $retVal;
}
function getFileData($file) {
$fp = fopen($file, "r");
$data = "";
while(!feof($fp)) {
$data .= fgets($fp, 1024);
}
fclose($fp);
return $data;
}
?>
谢谢。
答案 0 :(得分:0)
您可以放置HTML并调用变量。
创建一个for循环,例如
for ($i=0;$i<count($resEntity);$i++){
echo "<div class=\"item\"";
echo "<p> Name :" . $resEntity[$i]['name'] ."<br/> </p>";
echo "<p> Price:" . $resEntity[$i]['price'] ."<br/> </p>";
echo "<img src=\"/somepathhere/" . $resEntity[$i]['preview'] ."\"/><br/>";
echo "</div>";
}
根据您想要按钮的位置,如果您希望按钮位于每个项目上,则可以在for循环中添加。 如果没有,请在循环后执行。