Below is an example using PHP. The program consists of three files: spirit.php
defines related functions, list.php
displays the template list, and edit.php
edits the template. Each is explained below.
spirit.php
<?php
define("SPIRIT_HOST", "https://www.printspirit.cn");
define("UID", "third_test");
define("PASS", "third_test");
function getAccessToken($uid, $pass) {
$apcuAvailabe = function_exists('apcu_enabled') && apcu_enabled();
if($apcuAvailabe){
$access_token = apcu_fetch('access_token');
$expirt_time = apcu_fetch('expirt_time');
if ( $access_token && $expirt_time > time() ) return $access_token;
}
$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-access-token?userid=$uid&passwd=$pass"));
if ($rc!=NULL && $rc->rc=='OK') {
if($apcuAvailabe){
apcu_store('access_token', $rc->token);
apcu_store('expirt_time', time() + $rc->expirt);
}
return $rc->token;
}
die("Unable to get TOKEN:".$rc->errmsg);
}
function getList($subclass="") {
$token = getAccessToken(UID, PASS);
$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-label-list?token=${token}&subclass=${subclass}"), true);
if ($rc!=NULL && $rc['rc']=='OK') return $rc['data'];
return [];
}
function getContent($tpid) {
$token = getAccessToken(UID, PASS);
$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-label-content?token=${token}&tpid=${tpid}"), true);
if ($rc!=NULL && $rc['rc']=='OK') return $rc['data'];
return "";
}
function get_edit_url($subclass, $tpid="") {
$token = getAccessToken(UID, PASS);
return SPIRIT_HOST . "/third-edit?subclass=${subclass}&tpid=${tpid}&token=${token}";
}
This file defines four functions:
getAccessToken()
retrieves the access token for the API. Any API call requires an access_token. It is important to cache the access_token to avoid repeated retrieval within its validity period. In the example program, if PHP-APCu is installed, the token will be automatically cached.getList($subclass)
returns the list of print templates for the specified $subclass. If no class is specified, it returns all print templates under the account.get_edit_url($subclass, $tpid)
gets the URL for editing the template. If $tpid is empty, a new template is created. $subclass specifies the save category. You can directly jump to this address or embed it in an IFRAME.getContent($tpid)
retrieves the template content.
list.php
list.php
calls getList()
to get all print templates of the current user and displays them in a list. The thumbnail is displayed using /utils/thumb
. Each template has an edit button, and there is an add button at the bottom that calls edit.php
.
<?php
require_once("spirit.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A Test Client for PrintSpirit</title>
</head>
<body>
<table style="width:50%; margin: 0 auto; border:1px solid #888">
<tr><th>Label Name</th><th>Subclass</th><th>Thumbnail</th></tr>
<?php
$lst = getList();
foreach( $lst as $l) {
echo "<tr>";
echo "<td>${l['name']}</td>";
echo "<td>${l['subclass']}</td>";
/*Display thumbnail*/
echo "<td><img height='100px' src='" . SPIRIT_HOST . "/utils/thumb?id=${l[id]}' /></td>";
echo "<td><a href='edit.php?subclass=${l['subclass']}&tpid=${l['id']}"'><button>Edit (Embed)</button></a>"
echo "<a href='<?="edit.php?subclass=${l['subclass']}&tpid=${l['id']}&target=new"'><button>Edit (Redirect)</button></a></td>"
echo "</tr>\n";
}
?>
</table>
<div style="width:50%;margin:0 auto;padding:5px;text-align:right">
<a href='edit.php?subclass="user1"'><button>Add Label</button></a>
<div>
</body>
</html>
edit.php
Edit the print template by getting the edit URL through get_edit_url(subclass, tpid)
. You can directly redirect or embed it in an IFRAME. If tpid is empty, a new template is created. The subclass specifies the save category. In the example, all are saved to user1
. In an actual program, it can be set to the currently logged-in user of the third-party website.
<?php
require_once("spirit.php");
$url=get_edit_url($_GET['subclass'], $_GET['tpid']);
if (!empty($_GET['target']) && $_GET['target']=='new') {
header("Location: " . $url);
return;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div class="iframe">
<iframe src="<?php echo $url?>" />
</div>
</body>
</html>