Php 使用Spreadshirt API显示文章和签出链接

Php 使用Spreadshirt API显示文章和签出链接,php,api,curl,simplexml,Php,Api,Curl,Simplexml,我正在尝试使用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

我正在尝试使用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;
    }
?>

您只需放入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>";
}

对于($i=0;$i您只需放入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>";
}

用于($i=0;$iHey Yun,感谢您的回复。我对代码进行了大量修改,并提供了有关该产品的gotton详细信息,但我需要能够获取每个产品可用尺寸的下拉列表,以及每个产品的适当添加到购物篮/收银台url。嘿,Yun,感谢您的回复。我对代码进行了大量修改,并提供了gotton详细信息关于产品,但我需要能够抓取每个产品的可用尺寸下拉列表,以及每个产品的适当添加到购物篮/结帐url。