$product->appearance['id'] = $productTypeAppearanceId; $configuration = $product->configurations->configuration; $configuration->printArea['id'] = $productTypePrintAreaId; $configuration->printType['id'] = $printTypeId; $configuration->offset->x = (doubleval($printArea->boundary->size->width) - doubleval($designWidth)) / 2; $configuration->offset->y = (doubleval($printArea->boundary->size->height) - doubleval($designHeight)) / 4; $image = $product->configurations->configuration->content->svg->image; $image['width'] = $designWidth; $image['height'] = $designHeight; $image['designId'] = 'u' . $design['id']; $image['printColorIds'] = $printColorIds; // 9. Create product $attributes = $shop->products->attributes($namespaces['xlink']); $productsUrl = $attributes->href; $header = array(); $header[] = createSprdAuthHeader("POST", $productsUrl); $header[] = "Content-Type: application/xml"; $ch = curl_init($productsUrl); 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, $product->asXML()); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $result = curl_exec($ch); // Close the handle curl_close($ch); $productUrl = parseHttpHeaders($result, "Location"); $ch = curl_init($productUrl); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 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, true); $result = curl_exec($ch); curl_close($ch); echo json_encode("done"); } if ($action == "applyCoupon") { $apiKey = $_POST["apiKey"]; $coupon = array("code" => $_POST["couponCode"]); $data_string = json_encode($coupon); $basketCouponsURL = "api.spreadshirt." . $tld . "/api/v1/baskets/" . $basketId . "/coupons/?token=" . $apiKey; $header = array(); $header[] = createSprdAuthHeader("POST", $basketCouponsURL); $header[] = "Content-Type: application/json"; $ch = curl_init($basketCouponsURL); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string))); $result = curl_exec($ch); curl_close($ch); var_dump($result); } function createSprdAuthHeader($method, $url) { $apiKey = "";
// 2. Fetch design data to retrieve upload url $ch = curl_init($dataUrl); 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); $start = strpos($result, "resource xlink:href=\"") + 21; $end = strpos($result, "\"", $start); $imageUrl = substr($result, $start, $end - $start); echo "Image URL: " . $imageUrl . "\n"; // 3. Upload design data via image API $imageData = getFileData("design.png"); $header = array(); $header[] = createSprdAuthHeader("PUT", $imageUrl); $header[] = "Content-Type: image/png"; $ch = curl_init($imageUrl . "?method=PUT"); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POSTFIELDS, $imageData); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); function createSprdAuthHeader($method, $url) { $apiKey = "..."; $secret = "..."; $time = time() * 1000;