Advertisement
Guest User

Untitled

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