Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.93 KB | None | 0 0
  1. <?php
  2.  
  3. $session = "XXXXX";
  4. $apiKey = "XXXXX";
  5. $secret = "XXXXX";
  6. $useragent = "test"; // name of system like "Uploader" or similar things
  7.  
  8. define('SHOP_ID', '');
  9. define('USER_ID', 'XXXXX');
  10. define('SPREADSHIRT_API_KEY', $apiKey);
  11. define('SPREADSHIRT_API_SECRET', $secret);
  12. define('SESSION_ID', $session);
  13.  
  14.  $requestfile = "design.svg";
  15.  $quantityColors = 1; // how many colors are in
  16.  
  17. // 1. Create design entity via data api
  18. $url = "http://api.spreadshirt.com/api/v1/users/" . USER_ID . "/designs";
  19. $header = array();
  20. $header[] = createSprdAuthHeader("POST", $url);
  21. $header[] = "Content-Type: application/xml";
  22.  
  23. // Initialize handle and set options
  24. $ch = curl_init($url);
  25. curl_setopt($ch, CURLOPT_POST, true);
  26. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  27. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  28. curl_setopt($ch, CURLOPT_POSTFIELDS, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  29. <design xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net">
  30. <fileName></fileName>
  31. <name></name>
  32. <description></description>
  33. <restrictions>
  34. <colorCount>'.$quantityColors.'</colorCount>
  35. </restrictions>
  36. </design>');
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($ch, CURLOPT_HEADER, true);
  39. $result = curl_exec($ch);
  40. // Close the handle
  41. curl_close($ch);
  42.  
  43. echo $result . '<br />';
  44.  
  45. $dataUrl = parseHttpHeaders($result, "Location");
  46. echo "Design URL: $dataUrl<br />";
  47.  
  48. // 2. Fetch design data to retrieve upload url
  49. $header = array();
  50. $header[] = createSprdAuthHeader("GET", $dataUrl);
  51. $header[] = "Content-Type: application/xml";
  52.  
  53. $ch = curl_init($dataUrl);
  54. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($ch, CURLOPT_HEADER, false);
  57. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  58. $result = curl_exec($ch);
  59. // Close the handle
  60. curl_close($ch);
  61.  
  62. $designXml = simplexml_load_string($result);
  63. $imageUrl = $designXml->resources->resource[0]->attributes('xlink', TRUE);
  64. echo "Image URL: " . $imageUrl . "<br />";
  65.  
  66. // 3. Upload design data via image API
  67. $imageData = getFileData($requestfile);
  68.  
  69. $header = array();
  70.  
  71. $boundary = "moop" . md5(mt_rand() . microtime());
  72. $header[] = createSprdAuthHeader("PUT", $imageUrl);
  73. $header[] = "Content-Type: multipart/form-data; boundary={$boundary}";
  74.  
  75. $body[] = "--{$boundary}";
  76. $body[] = implode("\r\n", array(
  77. "Content-Disposition: form-data; name=\"filedata\"; filename=\"test.svg\"",
  78. "Content-Type: image/svg+xml",
  79. "",
  80. $imageData,
  81. ));
  82. $body[] = "--{$boundary}--";
  83.  
  84. //$header[] = "Content-Type: image/svg+xml";
  85.  
  86. $ch = curl_init($imageUrl . "?mediaType=svg&method=put");
  87. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($ch, CURLOPT_HEADER, false);
  90. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  91. curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\r\n", $body));
  92. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  93. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  94. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  95. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  96. $result = curl_exec($ch);
  97. curl_close($ch);
  98.  
  99. function createSprdAuthHeader($method, $url) {
  100.     $apiKey = SPREADSHIRT_API_KEY;
  101.     $secret = SPREADSHIRT_API_SECRET;
  102.     $session = SESSION_ID;
  103.     $time = time() * 1000;
  104.  
  105.     $data = "$method $url $time";
  106.     $sig = sha1("$data $secret");
  107.  
  108.     return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\", sessionId=\"$session\"";
  109. }
  110.  
  111. function parseHttpHeaders($header, $headername) {
  112.     $retVal = array();
  113.     $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
  114.     foreach ($fields as $field) {
  115.         if (preg_match('/(' . $headername . '): (.+)/m', $field, $match)) {
  116.             return $match[2];
  117.         }
  118.     }
  119.     return $retVal;
  120. }
  121.  
  122. function getFileData($file) {
  123.     return file_get_contents(__DIR__."/".$file);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement