Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace application\classes\spreadshirt;
- use application\controller\page\pub\spreadshirt\SpreadshirtDeisgnXMLController;
- /**
- * Description of SpreadshirtApi
- *
- * @author Adrian Knauff
- */
- class SpreadshirtApi {
- public static function test() {
- $uploadFilename = "img" . DIRECTORY_SEPARATOR . "IMG_3158.png";
- // The design upload script shows you how to create designs on the Spreadshirt platform
- // and upload the corresponding design data using Spreadshirt data and image API.
- // 1. Create design entity via data api
- $url = "https://api.spreadshirt.net/api/v1/shops/40000/designs";
- $header = [];
- $header[] = self::createSprdAuthHeader("POST", $url);
- $header[] = "Content-Type: application/xml";
- $designXmlFilename = "application" . DIRECTORY_SEPARATOR . "controller" . DIRECTORY_SEPARATOR . "page" . DIRECTORY_SEPARATOR . "pub" . DIRECTORY_SEPARATOR . "spreadshirt" . DIRECTORY_SEPARATOR . "design.xml";
- SpreadshirtDeisgnXMLController::saveDesignXML($designXmlFilename, "Design 233");
- $xml = self::getFileData($designXmlFilename);
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Adri Cli Shirtigo Uploader');
- 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);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- echo '<h2>Result 1</h2>';
- echo '<pre>';
- var_dump($result);
- echo '</pre>';
- if ($result === FALSE) {
- die("Result 1 ist false!");
- }
- $dataUrl = self::parseHttpHeaders($result, "Location");
- echo "<h2>Design URL:</h2>" . $dataUrl . "\n";
- //
- // 2. Fetch design data to retrieve upload url
- $ch = curl_init($dataUrl);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Adri Spreadshirt Uploader');
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
- $result = curl_exec($ch);
- curl_close($ch);
- echo '<h2>Result 2</h2>';
- echo '<pre>';
- var_dump($result);
- echo '</pre>';
- $start = strpos($result, "resource xlink:href=\"") + 21;
- $end = strpos($result, "\"", $start);
- $imageUrl = substr($result, $start, $end - $start);
- echo '<h2>image url</h2>';
- echo $imageUrl;
- // 3. Upload design data via image API
- $boundary = "moop" . md5(mt_rand() . microtime());
- if (!is_file($uploadFilename)) {
- die("UplaodFilename falsch: Datei nicht gefunden");
- }
- $imageData = self::getFileData($uploadFilename);
- $body = [];
- $body[] = "--{$boundary}";
- $body[] = implode("\r\n", array(
- "Content-Disposition: form-data; name=\"filedata\"; filename=\"test.png\"",
- "Content-Type: image/png",
- "",
- $imageData,
- ));
- $body[] = "--{$boundary}--";
- $header = [];
- $header[] = self::createSprdAuthHeader("PUT", $imageUrl);
- $header[] = "Content-Type: image/png";
- $ch = curl_init($imageUrl . "?method=PUT");
- curl_setopt($ch, CURLOPT_USERAGENT, 'Adri Spreadshirt Uploader');
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $imageData);
- // curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\r\n", $body));
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // curl_setopt($ch, CURLOPT_POST, true);
- // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- // curl_setopt($ch, CURLOPT_VERBOSE, 1);
- // curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
- // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
- // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:') );
- $result = curl_exec($ch);
- curl_close($ch);
- echo '<h2>Result 3</h2>';
- echo '<pre>';
- var_dump(htmlspecialchars($result));
- echo '<pre>';
- }
- private static function getFileData($file) {
- // $fp = fopen($file, "r");
- // $data = "";
- // while (!feof($fp)) {
- // $data .= fgets($fp, 1024);
- // }
- // fclose($fp);
- // return $data;
- return file_get_contents($file);
- }
- private static function parseHttpHeaders($header, $headername) {
- $retVal = [];
- $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;
- }
- private static function createSprdAuthHeader($method, $url) {
- $apiKey = "...";
- $secret = "...";
- $time = time() * 1000;
- $data = "$method $url $time";
- $sig = sha1("$data $secret");
- return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment