2012-03-11 22 views
2

我試圖使用Spreadshirt API顯示產品/文章。使用Spreadshirt API顯示文章和檢出鏈接

我現在需要最終添加一個下拉框來選擇每個產品的尺寸和一個按鈕以添加到購物籃並使用襯衫收銀。

<?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

你可以把你的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循環,你可以添加。 如果沒有,請在循環後執行。

+0

嘿Yun,謝謝你的回覆。我修改了很多代碼,並詳細介紹了該產品,但是我需要能夠獲取每個產品的可用尺寸下拉列表,以及每個產品適當的添加到購物籃/結帳網址。 – doubleplusgood 2012-03-20 23:15:36