Home > CMS Tutorial > DEDECMS > How dedecms writes API interface

How dedecms writes API interface

Release: 2019-07-31 10:56:28
Original
5856 people have browsed it

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}
Copy after login


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.&#39;/json.class.php&#39;);
$json = new Services_JSON(SERVICES_JSON_SUPPRESS_ERRORS);
echo $json->encode($reval);
Copy after login


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)
     )
Copy after login

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"}
  ]
Copy after login



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.&#39;/json.class.php&#39;);
$reval = array();
$dsql->SetQuery("SELECT id,title FROM `dede_archives` ORDER BY id DESC LIMIT 0,10");
$dsql->Execute(&#39;me&#39;);
while ($row = $dsql->GetArray(&#39;me&#39;)) {
$row[&#39;title&#39;] = gb2utf8($row[&#39;title&#39;]);
$reval[] = $row;
}
$json = new Services_JSON(SERVICES_JSON_SUPPRESS_ERRORS);
echo $json->encode($reval);
?>
Copy after login

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=&#39;http://域名/json.php&#39; cache=300}
[field:id/]-[field:title/]<br/>
{/dede:json}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template