下面以PHP为例子,给出一个实例。程序包括三个文件: spirit.php为定义相关的函数, list.php显示模板列表, edit.php编辑模板,下面分别讲解。
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("无法获取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}
该文件定义了4个函数:
getAccessToken()
获取访问API的token, 调用任何API都需要access_token 需要注意的是,应将access_token缓存起来,在有效期内避免重复获取, 在示例程序中如果安装了PHP-APCu将自动缓存token。getList($subclass)
返回$subclass指定类别的打印模板列表。如果不设定类别,返回该账户下的全部打印模板。get_edit_url($subclass, $tpid)
获取编辑模板的url地址,如$tpid为空,创建新模板。$subclass指定保存类别。可以直接跳转到该地址,或嵌入IFRAME。getContent($tpid)
获取模板内容
list.php
list.php
调用getList()
获取当前用户的全部打印模板,并列表显示出来,显示缩略图使用了/utils/thumb
。每个模板后面有一个编辑按钮,最下面有一个新增按钮就调用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>标签名</th><th>子分类</th><th>缩略图</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 /*显示缩略图*/
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>编辑(嵌入)</button></a>"
22 echo "<a href='<?="edit.php?subclass=${l['subclass']}&tpid=${l['id']}&target=new"'><button>编辑(跳转)</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>新增标签</button></a>
29 <div>
30 </body>
31</html>
edit.php
编辑打印模板,通过get_edit_url(subclass, tpid)获取编辑url, 可以直接跳转,或嵌入IFRAME中。如tpid为空,创建新模板。
subclass指定保存的子类,例子中全部保存到user1
,实际程序可以设置为第三方网站当前登录用户。
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>