deviant art

Deviant Login Shop  Join deviantART for FREE Take the Tour

Developers

Send Artwork to Sta.sh with PHP

The following PHP code samples (available on GitHub) show how you can upload a file to Sta.sh, create a folder for the file, and then rename that folder.

The code authenticates using our OAuth 2.0 draft 15 endpoints and sends the file to a user's Sta.sh via the /submit API call.

Submit a File
<?php

// Relies on the oAuth2 library by Pierrick Charron: https://github.com/adoy/PHP-OAuth2/
// git clone https://github.com/adoy/PHP-OAuth2.git /path/to/this/script/OAuth2
function __autoload($classname) {
    $name = preg_split('/\\\\/', $classname . '.php', -1, PREG_SPLIT_NO_EMPTY);
    require_once(implode(DIRECTORY_SEPARATOR, $name));
}

const CLIENT_ID = '0'; // OAuth 2.0 client_id
const CLIENT_SECRET = '0123456789abcdefghigklmnopqrstuv'; // OAuth 2.0 client_secret

const REDIRECT_URI = 'http://path.to/this/file';
const APPNAME = 'App.Name';

const AUTHORIZATION_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/authorize';
const TOKEN_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/token';
const SUBMIT_API = "https://www.deviantart.com/api/draft15/stash/submit";

try {
  $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC);
      if (!isset($_REQUEST['code'])) {
            $params = array('redirect_uri' => REDIRECT_URI);
            $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
            header('Location: ' . $auth_url);
            die('Redirecting ...');
      } else {
            $params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI);
            $response = $client->getAccessToken(TOKEN_ENDPOINT, OAuth2\Client::GRANT_TYPE_AUTH_CODE, $params);
            $val = (object) $response['result'];

            if (!$val) {
                  throw new Exception('No valid JSON response returned');
            }

            if (!$val->access_token) {
                  throw new Exception("No access token returned: ".$val->human_error);
            }

            $client->setAccessToken($val->access_token);

            $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_OAUTH);

            $response = $client->fetch(
                    SUBMIT_API,
                    array(
                        'title' => 'Fella Sample Image',
                        'artist_comments' => 'Fella Sample Image',
                        'keywords' => 'fella sample image',
                        'folder' => APPNAME,
                        'file' => "@fella.png"
                  ),
                  OAuth2\Client::HTTP_METHOD_POST
            );

            $result = (object) $response['result'];

            if (!$result) {
                  throw new Exception('No valid JSON response returned');
            }

            if ($result->status == 'success') {
                  print "Great Success! <a href=\"http://sta.sh/1{$result->stashid}\" target=\"_blank\">Stash ID {$result->stashid}</a>";
                  print "<br>Your submission is in the folder: {$result->folderid}";
            } else {
                  throw new Exception($result->human_error);
            }
      }
} catch (Exception $e) {
      print "Fatal Error: ".$e->getMessage();
}

?>
Move a submission to another folder (and create the folder)
<?php

// Relies on the oAuth2 library by Pierrick Charron: https://github.com/adoy/PHP-OAuth2/
// git checkout https://github.com/adoy/PHP-OAuth2.git /path/to/this/script/OAuth2
function __autoload($classname) {
    $name = preg_split('/\\\\/', $classname . '.php', -1, PREG_SPLIT_NO_EMPTY);
    require_once(implode(DIRECTORY_SEPARATOR, $name));
}

const CLIENT_ID = '0'; // OAuth 2.0 client_id
const CLIENT_SECRET = '0123456789abcdefghigklmnopqrstuv'; // OAuth 2.0 client_secret

const REDIRECT_URI = 'http://path.to/this/file';
const STASHID = '123465677'; // teh stashid parameter returned in the result of a submission
const NEW_FOLDER_NAME = 'Some other folder';

const AUTHORIZATION_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/authorize';
const TOKEN_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/token';
const SUBMIT_API = "https://www.deviantart.com/api/draft15/stash/move";

try {
  $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC);
      if (!isset($_REQUEST['code'])) {
            $params = array('redirect_uri' => REDIRECT_URI);
            $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
            header('Location: ' . $auth_url);
            die('Redirecting ...');
      } else {
            $params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI);
            $response = $client->getAccessToken(TOKEN_ENDPOINT, OAuth2\Client::GRANT_TYPE_AUTH_CODE, $params);
            $val = json_decode($response['result']);

            if (!$val) {
                  throw new Exception('No valid JSON response returned');
            }

            if (!$val->access_token) {
                  throw new Exception("No access token returned: ".$val->human_error);
            }

            $client->setAccessToken($val->access_token);

            $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_OAUTH);

            $response = $client->fetch(
                    SUBMIT_API,
                    array(
                        'stashid' => STASHID,
                        'folder' => NEW_FOLDER_NAME,
                  ),
                  OAuth2\Client::HTTP_METHOD_POST
            );

            $result = json_decode($response['result']);

            if (!$result) {
                  throw new Exception('No valid JSON response returned');
            }

            if ($result->status == 'success') {
                  print "Great Success! <a href=\"http://sta.sh/1{$result->stashid}\" target=\"_blank\">Stash ID {$result->stashid}</a>";
            } else {
                  throw new Exception($result->human_error);
            }
      }
} catch (Exception $e) {
      print "Fatal Error: ".$e->getMessage();
}

?>
Rename a Folder
<?php

// Relies on the oAuth2 library by Pierrick Charron: https://github.com/adoy/PHP-OAuth2/
// git checkout https://github.com/adoy/PHP-OAuth2.git /path/to/this/script/OAuth2
function __autoload($classname) {
    $name = preg_split('/\\\\/', $classname . '.php', -1, PREG_SPLIT_NO_EMPTY);
    require_once(implode(DIRECTORY_SEPARATOR, $name));
}

const CLIENT_ID = '0'; // OAuth 2.0 client_id
const CLIENT_SECRET = '0123456789abcdefghigklmnopqrstuv'; // OAuth 2.0 client_secret

const REDIRECT_URI = 'http://path.to/this/file';
const NEW_FOLDER_NAME = 'New Folder Name';
const FOLDERID = '601234'; // use the folderid returned in the result of a submission

const AUTHORIZATION_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/authorize';
const TOKEN_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/token';
const FOLDER_API = "https://www.deviantart.com/api/draft15/stash/folder";

try {
  $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC);
      if (!isset($_REQUEST['code'])) {
            $params = array('redirect_uri' => REDIRECT_URI);
            $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
            header('Location: ' . $auth_url);
            die('Redirecting ...');
      } else {
            $params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI);
            $response = $client->getAccessToken(TOKEN_ENDPOINT, OAuth2\Client::GRANT_TYPE_AUTH_CODE, $params);
            $val = json_decode($response['result']);

            if (!$val) {
                  throw new Exception('No valid JSON response returned');
            }

            if (!$val->access_token) {
                  throw new Exception("No access token returned: ".$val->human_error);
            }

            $client->setAccessToken($val->access_token);

            $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_OAUTH);

            $response = $client->fetch(
                    FOLDER_API,
                    array(
                        'folderid' => FOLDERID,
                        'folder' => NEW_FOLDER_NAME,
                  ),
                  OAuth2\Client::HTTP_METHOD_POST
            );

            $result = json_decode($response['result']);

            if (!$result) {
                  throw new Exception('No valid JSON response returned');
            }

            if ($result->status == 'success') {
                  print "Great Success! Your folder '".FOLDERID."' has been renamed to '".NEW_FOLDER_NAME."'.";
            } else {
                  throw new Exception($result->human_error);
            }
      }
} catch (Exception $e) {
      print "Fatal Error: ".$e->getMessage();
}

?>