開發一個網站客戶端有很多中辦法,最簡單的就是后臺什么也不便,安卓客戶端只是根據URL地址獲得整個網頁放到webview中,但這種沒什么意義,可以更進一步,仍然只是顯示完整網頁,但是為客戶端專門布局html。
但真正做的好的客戶端不是這么敷衍了事,php服務端只提供數據,而不負責數據的顯示,所有顯示在手機上完成。而服務器數據一般都是返回xml或者是 json。
遺憾的是dedecms到現在為止都沒有推出任何平臺的客戶端,服務端根本連個接口都沒有。
本文介紹如何為安卓客戶端提供后臺支持,返回xml數據。
這里以獲取文章詳情為例。
思路:其實為客戶端提供數據我們只需改變返回的數據格式,將原本的html改成xml格式,也就是說我們只需要一個xml格式的模版,其他代碼可以不變。因為不管是安卓客戶端還是網頁,對于一篇文章而言,我們需要的信息基本是一致的。
問題是,既然我們需要為安卓客戶采用專門的模版,如何讓dede識別到請求來自安卓客戶端,且在什么地方加載這個模版呢?
識別來自安卓客戶端只需在客戶端的請求里面加入一個標志位就行了,我這里的標志名稱為isMobile,然后在dede的/plus/view.php文件中獲得這個變量:
if($isMobile==1)$arc->setMobileVisitFlag(true);else{$arc->setMobileVisitFlag(false);} |
isMobile變量可以直接獲取到,不需要自己處理,dede對所有請求做了處理,將每個請求的值映射到了和請求名稱完全一致的變量中。
上面的代碼獲得了這個變量,判斷是否為1,如果為一表示來自于客戶端,這時我們調用一個我自己在/include/arc.archives.class.php文件中加入的函數setMobileVisitFlag()。setMobileVisitFlag()函數是這樣定義的
public function setMobileVisitFlag($flag){$this->mobileVisitFlag = $flag;} |
其中$mobileVisitFlag為我們給Archives類增加的一個變量。
現在我們需要找到Archives類中加載模版文件的地方,在這里對mobileVisitFlag的情況做判斷,如果為真,則模板換成我們的xml模版,也就是客戶端需要的數據格式。代碼在GetTempletFile()函數中。下面是GetTempletFile原來的代碼:
function GetTempletFile(){global $cfg_basedir,$cfg_templets_dir,$cfg_df_style;$cid = $this->ChannelUnit->ChannelInfos['nid'];if(!empty($this->Fields['templet'])){$filetag = MfTemplet($this->Fields['templet']);if( !preg_match("#//#", $filetag) ) $filetag = $GLOBALS['cfg_df_style'].'/'.$filetag;}else{$filetag = MfTemplet($this->TypeLink->TypeInfos["temparticle"]);}$tid = $this->Fields['typeid'];$filetag = str_replace('{cid}', $cid,$filetag);$filetag = str_replace('{tid}', $tid,$filetag);$tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;if($cid=='spec'){if( !empty($this->Fields['templet']) ){$tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;}else{$tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_spec.htm";}}if(!file_exists($tmpfile)){$tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/".($cid=='spec' ? 'article_spec.htm' : 'article_default.htm');}if (!preg_match("#.htm$#", $tmpfile)) return FALSE;return $tmpfile;} |
改成這樣
function GetTempletFile() { global $cfg_basedir,$cfg_templets_dir,$cfg_df_style; $cid = $this->ChannelUnit->ChannelInfos['nid']; if(!empty($this->Fields['templet'])) { $filetag = MfTemplet($this->Fields['templet']); if( !preg_match("#//#", $filetag) ) $filetag = $GLOBALS['cfg_df_style'].'/'.$filetag; } else { $filetag = MfTemplet($this->TypeLink->TypeInfos["temparticle"]); } $tid = $this->Fields['typeid']; $filetag = str_replace('{cid}', $cid,$filetag); $filetag = str_replace('{tid}', $tid,$filetag); /*HEJIE_MODIFY 客戶端 @www.jcodecraeer.com*/ if($this->mobileVisitFlag ){ header("Content-Type:text/xml; charset=GBK"); $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_detail_mobile.xml"; return $tmpfile; } $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag; if($cid=='spec') { if( !empty($this->Fields['templet']) ) { $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag; } else { $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_spec.htm"; } } if(!file_exists($tmpfile)) { $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/".($cid=='spec' ? 'article_spec.htm' : 'article_default.htm'); } if (!preg_match("#.htm$#", $tmpfile)) return FALSE; return $tmpfile; } |
紅色部分為加入的代碼。
最后是這個xml模版的寫法了,這個模版基本上是按照一種約定來寫的,跟客戶端如何解析xml相關,這里只是給出一個我自己的例子
<?xml version="1.0" encoding="UTF-8"?><jcodecraeer><blog><id>{dede:field.id/}</id><title><![CDATA[{dede:field.title/}]]></title><author>{dede:field.authoranme/}</author><url>http://www.jcodecraeer.com{dede:field.arcurl/}</url><body><![CDATA[<h4> {dede:field.title/} </h4><div><span style='color:#333333;font-size:12px;padding-right:15px;'>{dede:field.authoranme/}</span><span style='color:#333333;font-size:12px;padding-right:15px;'> {dede:field.pubdate function="MyDate('Y-m-d H:i:s',@me)"/}</span></div>{dede:field.body /}]]></body><pubDate>{dede:field.pubdate function="MyDate('Y-m-d H:i:s',@me)"/}</pubDate><commentCount>{dede:field.id runphp=’yes’}$dsql = new dedesql(false);$dsql -> SetQuery(“Select count(id) as c from dede_feedback where aid=”.@me);$row = $dsql -> getone();@me=$row['c'];{/dede:field.id}</commentCount></blog></jcodecraeer> |
新聞熱點
疑難解答