Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/anarchoi/public_html/test.php:102 Stack trace: #0 /home/anarchoi/public_html/test.php(102): SimpleXMLElement->__construct('') #1 {main} thrown in /home/anarchoi/public_html/test.php on line 102
  2.  
  3. $productTypeId = "210";
  4. $printTypeId = "17";
  5. $printColorIds = "13,20";
  6. $productTypeAppearanceId = "1";
  7. $productTypePrintAreaId = "4";
  8. $designId = "10438193";
  9.  
  10. // 1. Get shop data
  11. $shopUrl = "http://api.spreadshirt.com/api/v1/shops/266497";
  12. $ch = curl_init($shopUrl);
  13. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_HEADER, false);
  16. $result = curl_exec($ch);
  17. // Close the handle
  18. curl_close($ch);
  19.  
  20. $shop = new SimpleXMLElement($result);
  21. $namespaces = $shop->getNamespaces(true);
  22.  
  23. // 2. Get product type data
  24. $attributes = $shop->productTypes->attributes($namespaces['xlink']);
  25. $productTypeUrl = $attributes->href . "/" . $productTypeId;
  26. $ch = curl_init($productTypeUrl);
  27. // echo "<br>$productTypeUrl<br>";
  28. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  30. curl_setopt($ch, CURLOPT_HEADER, false);
  31. $result = curl_exec($ch);
  32. // Close the handle
  33. curl_close($ch);
  34.  
  35. $productType = new SimpleXMLElement($result);
  36.  
  37. // 3. Get design data
  38. $attributes = $shop->designs->attributes($namespaces['xlink']);
  39. $designUrl = $attributes->href . "/" . $designId;
  40. $ch = curl_init($designUrl);
  41. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  43. curl_setopt($ch, CURLOPT_HEADER, false);
  44. $result = curl_exec($ch);
  45. // Close the handle
  46. curl_close($ch);
  47.  
  48. $design = new SimpleXMLElement($result);
  49.  
  50. // 4. Prepare product
  51. // get positioning data for selected product type
  52. $printArea = null;
  53. foreach ($productType->printAreas->printArea as $current) {
  54. if ($current['id'] == $productTypePrintAreaId) {
  55. $printArea = $current;
  56. }
  57. }
  58.  
  59. $product = new SimpleXMLElement(getFileData("product.xml"));
  60. $product->productType['id'] = $productTypeId;
  61. $product->appearance['id'] = $productTypeAppearanceId;
  62. $configuration = $product->configurations->configuration;
  63. $configuration->printArea['id'] = $productTypePrintAreaId;
  64. $configuration->printType['id'] = $printTypeId;
  65. $configuration->offset->x =
  66. ((doubleval($printArea->boundary->size->width) - doubleval($design->size->width)) / 2);
  67. $configuration->offset->y =
  68. ((doubleval($printArea->boundary->size->height) - doubleval($design->size->height)) / 4);
  69. $image = $product->configurations->configuration->content->svg->image;
  70. $image['width'] = $design->size->width;
  71. $image['height'] = $design->size->height;
  72. $image['designId'] = $designId;
  73. $image['printColorIds'] = $printColorIds;
  74.  
  75. // 5. Create product
  76. $attributes = $shop->products->attributes($namespaces['xlink']);
  77. $productsUrl = $attributes->href;
  78.  
  79. $header = array();
  80. $header[] = createSprdAuthHeader("POST", $productsUrl);
  81. $header[] = "Content-Type: application/xml";
  82.  
  83. $ch = curl_init("$productsUrl"."?fullData=true");
  84. curl_setopt($ch, CURLOPT_POST, true);
  85. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  86. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, $product->asXML());
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($ch, CURLOPT_HEADER, true);
  90. $result = curl_exec($ch);
  91. // Close the handle
  92. curl_close($ch);
  93. $productUrl = parseHttpHeaders($result, "Location");
  94.  
  95. $ch = curl_init($productUrl);
  96. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  98. curl_setopt($ch, CURLOPT_HEADER, false);
  99. $result = curl_exec($ch);
  100. // Close the handle
  101. curl_close($ch);
  102.  
  103. $product = new SimpleXMLElement($result);
  104. $resource = $product->resources->resource[0];
  105. $attributes = $resource->attributes($namespaces['xlink']);
  106.  
  107. echo '<html><body>';
  108. echo 'Product available at: <a href="' . $productUrl . '">' . $productUrl . '</a></br>';
  109. echo 'Product image is available at: <a href="' . $attributes->href . '">' . $attributes->href . '</a></br>';
  110. echo '<img src="' . $attributes->href . '?width=1000"/>';
  111. echo '</body></html>';
  112.  
  113. function createSprdAuthHeader($method, $url) {
  114. $apiKey = "***";
  115. $secret = "***";
  116. $time = time()*1000;
  117.  
  118. $data = "$method $url $time";
  119. $sig = sha1("$data $secret");
  120.  
  121. return "Authorization: SprdAuth apiKey="$apiKey", data="$data", sig="$sig"";
  122. }
  123.  
  124. function parseHttpHeaders( $header, $headername ) {
  125. $retVal = array();
  126. $fields = explode("rn", preg_replace('/x0Dx0A[x09x20]+/', ' ', $header));
  127. foreach( $fields as $field ) {
  128. if( preg_match('/('.$headername.'): (.+)/m', $field, $match) ) {
  129. return $match[2];
  130. }
  131. }
  132. return $retVal;
  133. }
  134.  
  135. function getFileData($file) {
  136. $fp = fopen($file, "r");
  137. $data = "";
  138. while(!feof($fp)) {
  139. $data .= fgets($fp, 1024);
  140. }
  141. fclose($fp);
  142. return $data;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement