Guest User

Shirtigo API Test 1

a guest
Nov 22nd, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. <?php
  2.  
  3. namespace application\classes\spreadshirt;
  4.  
  5. use application\controller\page\pub\spreadshirt\SpreadshirtDeisgnXMLController;
  6.  
  7. /**
  8.  * Description of SpreadshirtApi
  9.  *
  10.  * @author Adrian Knauff
  11.  */
  12. class SpreadshirtApi {
  13.  
  14.     public static function test() {
  15.  
  16.         $uploadFilename = "img" . DIRECTORY_SEPARATOR . "IMG_3158.png";
  17.  
  18.         // The design upload script shows you how to create designs on the Spreadshirt platform
  19.         // and upload the corresponding design data using Spreadshirt data and image API.
  20.         // 1. Create design entity via data api
  21.         $url = "https://api.spreadshirt.net/api/v1/shops/40000/designs";
  22.         $header = [];
  23.         $header[] = self::createSprdAuthHeader("POST", $url);
  24.         $header[] = "Content-Type: application/xml";
  25.  
  26.         $designXmlFilename = "application" . DIRECTORY_SEPARATOR . "controller" . DIRECTORY_SEPARATOR . "page" . DIRECTORY_SEPARATOR . "pub" . DIRECTORY_SEPARATOR . "spreadshirt" . DIRECTORY_SEPARATOR . "design.xml";
  27.         SpreadshirtDeisgnXMLController::saveDesignXML($designXmlFilename, "Design 233");
  28.  
  29.         $xml = self::getFileData($designXmlFilename);
  30.  
  31.         $ch = curl_init($url);
  32.         curl_setopt($ch, CURLOPT_USERAGENT, 'Adri Cli Shirtigo Uploader');
  33.         curl_setopt($ch, CURLOPT_POST, true);
  34.         curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  35.         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  36.         curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  37.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38.         curl_setopt($ch, CURLOPT_HEADER, true);
  39.         $result = curl_exec($ch);
  40.         curl_close($ch);
  41.         echo '<h2>Result 1</h2>';
  42.         echo '<pre>';
  43.         var_dump($result);
  44.         echo '</pre>';
  45.         if ($result === FALSE) {
  46.             die("Result 1 ist false!");
  47.         }
  48.         $dataUrl = self::parseHttpHeaders($result, "Location");
  49.  
  50.         echo "<h2>Design URL:</h2>" . $dataUrl . "\n";
  51.  
  52.         //
  53.         // 2. Fetch design data to retrieve upload url
  54.         $ch = curl_init($dataUrl);
  55.         curl_setopt($ch, CURLOPT_USERAGENT, 'Adri Spreadshirt Uploader');
  56.         curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  57.         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  58.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  59.         curl_setopt($ch, CURLOPT_HEADER, FALSE);
  60.         $result = curl_exec($ch);
  61.         curl_close($ch);
  62.         echo '<h2>Result 2</h2>';
  63.         echo '<pre>';
  64.         var_dump($result);
  65.         echo '</pre>';
  66.  
  67.         $start = strpos($result, "resource xlink:href=\"") + 21;
  68.         $end = strpos($result, "\"", $start);
  69.         $imageUrl = substr($result, $start, $end - $start);
  70.  
  71.  
  72.         echo '<h2>image url</h2>';
  73.         echo $imageUrl;
  74.  
  75.         // 3. Upload design data via image API
  76.         $boundary = "moop" . md5(mt_rand() . microtime());
  77.  
  78.         if (!is_file($uploadFilename)) {
  79.             die("UplaodFilename falsch: Datei nicht gefunden");
  80.         }
  81.         $imageData = self::getFileData($uploadFilename);
  82.         $body = [];
  83.         $body[] = "--{$boundary}";
  84.         $body[] = implode("\r\n", array(
  85.             "Content-Disposition: form-data; name=\"filedata\"; filename=\"test.png\"",
  86.             "Content-Type: image/png",
  87.             "",
  88.             $imageData,
  89.         ));
  90.         $body[] = "--{$boundary}--";
  91.  
  92.  
  93.         $header = [];
  94.         $header[] = self::createSprdAuthHeader("PUT", $imageUrl);
  95.         $header[] = "Content-Type: image/png";
  96.         $ch = curl_init($imageUrl . "?method=PUT");
  97.         curl_setopt($ch, CURLOPT_USERAGENT, 'Adri Spreadshirt Uploader');
  98.         curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  99.         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  100.         curl_setopt($ch, CURLOPT_POSTFIELDS, $imageData);
  101. //        curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\r\n", $body));
  102.         curl_setopt($ch, CURLOPT_HEADER, true);
  103.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  104.  
  105. //        curl_setopt($ch, CURLOPT_POST, true);
  106. //        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  107. //        curl_setopt($ch, CURLOPT_VERBOSE, 1);
  108. //        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  109. //        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  110. //        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  111. //        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  112. //        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:') );
  113.  
  114.         $result = curl_exec($ch);
  115.         curl_close($ch);
  116.  
  117.         echo '<h2>Result 3</h2>';
  118.         echo '<pre>';
  119.         var_dump(htmlspecialchars($result));
  120.         echo '<pre>';
  121.     }
  122.  
  123.     private static function getFileData($file) {
  124. //        $fp = fopen($file, "r");
  125. //        $data = "";
  126. //        while (!feof($fp)) {
  127. //            $data .= fgets($fp, 1024);
  128. //        }
  129. //        fclose($fp);
  130. //        return $data;
  131.  
  132.         return file_get_contents($file);
  133.     }
  134.  
  135.     private static function parseHttpHeaders($header, $headername) {
  136.         $retVal = [];
  137.         $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
  138.         foreach ($fields as $field) {
  139.             if (preg_match('/(' . $headername . '): (.+)/m', $field, $match)) {
  140.                 return $match[2];
  141.             }
  142.         }
  143.         return $retVal;
  144.     }
  145.  
  146.     private static function createSprdAuthHeader($method, $url) {
  147.         $apiKey = "...";
  148.         $secret = "...";
  149.         $time = time() * 1000;
  150.  
  151.         $data = "$method $url $time";
  152.         $sig = sha1("$data $secret");
  153.  
  154.         return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment