Home > Article > CMS Tutorial > How dedecms writes API interface
json data format can facilitate data call and reference between different sites. Of course, our DEDECMS can also generate JSON for the whole site data for other sites to call. The code is very simple and mainly used include/json.class.php.
Dreamweaver itself has its own json tag. The calling method:
{dede:json url='http://yoursite/json.php' cache=300} [field:id/]-[field:title/]<br/> {/dede:json}
This tag calling example has been provided to us in the Dreamweaver manual. URL is a remote json interface address. In the json.php code of this interface file, the final return must be to pass the data through the json_encode($feeds) system function. After json encoding, print it out through the echo or print() function. This Two points are necessary. Then, we can get the data through $.ajax() or $.getjson() in the foreground. The DreamWeaver system provides us with a json class in the include/json.class.php file. That is to say, when we convert the json encoding of the php file, we have two methods:
1. Directly use the system function json_encode() provided by the PHP system. I encourage everyone to use this, which is simple and trouble-free. Since the PHP system provides it to us, we can not use the one provided by the DreamWeaver system.
2. Use the encode() provided by the DreamWeaver system. Before using it, first introduce json.class.php, that is:
require_once(DEDEINC.'/json.class.php'); $json = new Services_JSON(SERVICES_JSON_SUPPRESS_ERRORS); echo $json->encode($reval);
The variable $reval is what we get from the database or other places. It is usually a two-dimensional array, for example:
Array ( [0] => Array ( [id] => 95 [title] => 原图设计) [1] => Array ( [id] => 113 [title] => ssssssssssss) [2] => Array ( [id] => 111 [title] => hjhj ) [3] => Array ( [id] => 110 [title] => ssssssssssss) )
After echo, the displayed content is as follows.
[ {"id":"95","title":"\u539f\u521b"}, {"id":"113","title":"ssssssssssss"}, {"id":"111","title":"hjhj"}, {"id":"110","title":"ssssssssssss"} ]
This is the content displayed after encoding() or using json_encode(). That is, several json data enclosed in square brackets are returned to the requested $.ajax() or $.getjson(), which processes the data and displays the results we want.
Now that we know the principle, the next step is the detailed implementation method, as follows:
First create a new PHP file and name it json.php (you can also create a new folder and name it is api, and then PHP is named index.php, so when calling, you only need to call it like http://your domain name/api), which is used as the called API interface. The code is as follows:
<?php $cfg_NotPrintHead = false; header("Content-Type: text/html; charset=utf-8"); include_once (dirname(__FILE__)."/../include/common.inc.php"); error_reporting(E_ALL || ~E_NOTICE); require_once(DEDEINC.'/json.class.php'); $reval = array(); $dsql->SetQuery("SELECT id,title FROM `dede_archives` ORDER BY id DESC LIMIT 0,10"); $dsql->Execute('me'); while ($row = $dsql->GetArray('me')) { $row['title'] = gb2utf8($row['title']); $reval[] = $row; } $json = new Services_JSON(SERVICES_JSON_SUPPRESS_ERRORS); echo $json->encode($reval); ?>
The code here has been converted from GBK to UTF8, so it is compatible with the GBK version of DEDECMS
Calling method:
{dede:json url='http://域名/json.php' cache=300} [field:id/]-[field:title/]<br/> {/dede:json}
Just put the calling code where you need it
cache=300 cache time, 0 means no caching
Recommended: dedecms usage tutorial
The above is the detailed content of How dedecms writes API interface. For more information, please follow other related articles on the PHP Chinese website!