Below is a PHP example consisting of three files: spirit.php defines related functions, list.php displays template lists, and edit.php handles template editing. Each will be explained in detail.

spirit.php

 1<?php
 2define("SPIRIT_HOST", "https://www.printspirit.cn");
 3define("UID",  "third_test");
 4define("PASS", "third_test");
 5
 6function getAccessToken($uid, $pass) {
 7
 8	$apcuAvailabe = function_exists('apcu_enabled') && apcu_enabled();
 9	
10	if($apcuAvailabe){
11		$access_token = apcu_fetch('access_token');
12		$expirt_time = apcu_fetch('expirt_time');
13		if ( $access_token && $expirt_time > time() ) return $access_token;
14	}
15
16	$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-access-token?userid=$uid&passwd=$pass"));
17
18	if ($rc!=NULL && $rc->rc=='OK') {
19		if($apcuAvailabe){
20			apcu_store('access_token', $rc->token);
21			apcu_store('expirt_time',  time() + $rc->expirt);
22		}
23		return $rc->token;
24	}	
25	die("Failed to obtain TOKEN:".$rc->errmsg);
26}
27
28function getList($subclass="") {
29	$token = getAccessToken(UID, PASS);
30	$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-label-list?token=${token}&subclass=${subclass}"), true);
31	if ($rc!=NULL && $rc['rc']=='OK') 	return $rc['data'];
32	return [];
33}
34
35function getContent($tpid) {
36	$token = getAccessToken(UID, PASS);
37	$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-label-content?token=${token}&tpid=${tpid}"), true);
38	if ($rc!=NULL && $rc['rc']=='OK') return $rc['data'];
39	return "";
40}
41
42function get_edit_url($subclass, $tpid="") {
43	$token = getAccessToken(UID, PASS);
44	return SPIRIT_HOST . "/third-edit?subclass=${subclass}&tpid=${tpid}&token=${token}";	
45}

This file defines four functions:

  • getAccessToken() retrieves the access token for API calls. Note: The access token should be cached to avoid repeated fetches during 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 under the specified $subclass category. If no category is specified, returns all print templates under the account.
  • get_edit_url($subclass, $tpid) generates the URL for editing templates. If $tpid is empty, creates a new template. $subclass specifies the save category. Users can either redirect to this URL or embed it in an IFRAME.
  • getContent($tpid) retrieves template content.

list.php

list.php calls getList() to display all print templates of the current user, using /utils/thumb to show thumbnails. Each template has an edit button, with an "Add New" button at the bottom calling edit.php.

 1<?php
 2require_once("spirit.php");
 3?>
 4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5<html xmlns="http://www.w3.org/1999/xhtml">
 6  <head>
 7    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 8    <title>A Test Client for Spirit</title>
 9  </head>
10  <body>
11  	<table style="width:50%; margin: 0 auto; border:1px solid #888">
12  	<tr><th>Label Name</th><th>Subclass</th><th>Thumbnail</th></tr>
13  	<?php 
14			$lst = getList();
15  		foreach( $lst as $l) {
16	  		echo "<tr>";
17	  		echo "<td>${l['name']}</td>";
18	  		echo "<td>${l['subclass']}</td>";
19	  		/*Display thumbnail*/
20	  		echo "<td><img height='100px' src='" . SPIRIT_HOST . "/utils/thumb?id=${l[id]}' /></td>";
21	  		echo "<td><a href='edit.php?subclass=${l['subclass']}&tpid=${l['id']}"'><button>Edit (Embedded)</button></a>"
22	      	echo "<a href='<?="edit.php?subclass=${l['subclass']}&tpid=${l['id']}&target=new"'><button>Edit (Redirect)</button></a></td>"
23	  		echo "</tr>\n";
24	  	}
25  	?>	
26  	</table>
27  	<div style="width:50%;margin:0 auto;padding:5px;text-align:right">
28  		<a href='edit.php?subclass="user1"'><button>New Label</button></a>
29  	<div>
30  </body>
31</html> 

edit.php

Edits print templates by obtaining the editing URL through get_edit_url(subclass, tpid). Users can either redirect directly or embed the URL in an IFRAME. If tpid is empty, creates a new template. The subclass parameter specifies the save category (set to user1 in this example; real applications can use the third-party website's current logged-in user).

 1<?php
 2require_once("spirit.php");
 3$url=get_edit_url($_GET['subclass'], $_GET['tpid']);
 4
 5if (!empty($_GET['target']) && $_GET['target']=='new') {
 6    header("Location: " . $url);
 7    return;
 8}
 9?>
10<html xmlns="http://www.w3.org/1999/xhtml">
11  <body>
12  <div class="iframe">
13     <iframe src="<?php echo $url?>" />
14  </div>
15  </body>
16</html> 

Leave Your Message

login