Advertisement
Guest User

spreadshirt design listing

a guest
Dec 27th, 2017
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     // This script shows you how to retrieve an design list using Spreadshirt API v1
  3.     // and render it with html.
  4.  
  5.     $apiKey = "XXXXX"; // Spreadshirt API Key
  6.     $apiSecret = "XXXXX"; // Spreadshirt API Secret
  7.     $useragent = "XXXXX"; // Name of system like "Uploader" or similar things
  8.  
  9.  
  10.  
  11.  
  12.     $query = isset($_REQUEST['query']) ? $_REQUEST['query'] : '';
  13.  
  14.  
  15.     $header = array();
  16.     $header[] = createSprdAuthHeader("GET", $url, $apiKey, $apiSecret);
  17.     $header[] = "Content-Type: application/xml";
  18.  
  19.     // 1. Get shop
  20.     $shopUrl = "http://api.spreadshirt.net/api/v1/shops/205909";
  21.     $ch = curl_init($shopUrl);
  22.     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  23.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24.     curl_setopt($ch, CURLOPT_HEADER, false);
  25.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  26.     curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  27.     $result = curl_exec($ch);
  28.     // Close the handle
  29.     curl_close($ch);
  30.  
  31.     $shop = new SimpleXMLElement($result);
  32.     $namespaces = $shop->getNamespaces(true);
  33.  
  34.     // 2. Get designs
  35.     $attributes = $shop->designs->attributes($namespaces['xlink']);
  36.     $designsUrl = $attributes->href;
  37.     $ch = curl_init($designsUrl . "?limit=100&query=". $query);
  38.     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  39.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  40.     curl_setopt($ch, CURLOPT_HEADER, false);
  41.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  42.     curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  43.     $result = curl_exec($ch);
  44.     // Close the handle
  45.     curl_close($ch);
  46.  
  47.     $designs = new SimpleXMLElement($result);
  48.  
  49.     // 3. Print design list
  50.     echo '<html>';
  51.     echo '<head>';
  52.     echo '<title>Design List Rendering Example</title>';
  53.     echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>';
  54.     echo '</head>';
  55.     echo '<body>';
  56.     echo '<div>';
  57.     echo '<form action="design-listing.php">';
  58.     echo '<input type="text" id="query" name="query" value="' . $query . '" style="font-size: 15px; font-weight: bold; width: 400px;"/><input type="submit" value="Search"/>';
  59.     echo '</form></div>';
  60.     echo '<div>';
  61.     foreach ($designs->design as $design) {
  62.         $resource = $design->resources->resource[0];
  63.         $attributes = $resource->attributes($namespaces['xlink']);
  64.         echo '<div style="float: left; width: 150px; height: 150px;"><img src="' . $attributes->href . '?width=150&height=150" width="150" height="150"/></div>';
  65.     }
  66.     echo '</div>';
  67.     echo '</body>';
  68.     echo '</html>';
  69.  
  70.  
  71.  
  72.  
  73. function createSprdAuthHeader($method, $url, $apiKey, $secret) {
  74.     $session = session_id();
  75.     $time = time() * 1000;
  76.  
  77.     $data = "$method $url $time";
  78.     $sig = sha1("$data $secret");
  79.  
  80.     return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\", sessionId=\"$session\"";
  81. }
  82.  
  83. ?>
Advertisement
RAW Paste Data Copied
Advertisement