Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $session = "XXXXX"; // replace with a valid session
- $apiKey = "XXXXX";
- $secret = "XXXXX";
- $useragent = "XXXXX"; // name of system like "Uploader" or similar things
- define('SHOP_ID', 'XXXXX');
- define('USER_ID', 'XXXXX');
- define('SPREADSHIRT_API_KEY', $apiKey);
- define('SPREADSHIRT_API_SECRET', $secret);
- define('SESSION_ID', $session);
- $requestfile = "design.png";
- // 1. Create design entity via data api
- $url = "http://api.spreadshirt.com/api/v1/users/" . USER_ID . "/designs";
- $header = array();
- $header[] = createSprdAuthHeader("POST", $url);
- $header[] = "Content-Type: application/xml";
- // Initialize handle and set options
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <design xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net">
- <fileName></fileName>
- <name></name>
- <description></description>
- <restrictions>
- </restrictions>
- </design>');
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- $result = curl_exec($ch);
- // Close the handle
- curl_close($ch);
- echo $result . '<br />';
- $dataUrl = parseHttpHeaders($result, "Location");
- echo "Design URL: $dataUrl<br />";
- // 2. Fetch design data to retrieve upload url
- $header = array();
- $header[] = createSprdAuthHeader("GET", $dataUrl);
- $header[] = "Content-Type: application/xml";
- $ch = curl_init($dataUrl);
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $result = curl_exec($ch);
- // Close the handle
- curl_close($ch);
- $designXml = simplexml_load_string($result);
- $imageUrl = $designXml->resources->resource[0]->attributes('xlink', TRUE);
- echo "Image URL: " . $imageUrl . "<br />";
- // 3. Upload design data via image API
- $imageData = getFileData($requestfile);
- $header = array();
- $boundary = "moop" . md5(mt_rand() . microtime());
- $header[] = createSprdAuthHeader("PUT", $imageUrl);
- $header[] = "Content-Type: multipart/form-data; boundary={$boundary}";
- $body[] = "--{$boundary}";
- $body[] = implode("\r\n", array(
- "Content-Disposition: form-data; name=\"filedata\"; filename=\"test.png\"",
- "Content-Type: image/png",
- "",
- $imageData,
- ));
- $body[] = "--{$boundary}--";
- //$header[] = "Content-Type: image/svg+xml";
- $ch = curl_init($imageUrl . "?method=put");
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\r\n", $body));
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
- $result = curl_exec($ch);
- curl_close($ch);
- function createSprdAuthHeader($method, $url) {
- $apiKey = SPREADSHIRT_API_KEY;
- $secret = SPREADSHIRT_API_SECRET;
- $session = SESSION_ID;
- $time = time() * 1000;
- $data = "$method $url $time";
- $sig = sha1("$data $secret");
- return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\", sessionId=\"$session\"";
- }
- function parseHttpHeaders($header, $headername) {
- $retVal = array();
- $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
- foreach ($fields as $field) {
- if (preg_match('/(' . $headername . '): (.+)/m', $field, $match)) {
- return $match[2];
- }
- }
- return $retVal;
- }
- function getFileData($file) {
- return file_get_contents(__DIR__."/".$file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement