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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

Empire CMS template download location: Official template download: https://www.phome.net/template/ Third-party template website: https://www.dedecms.com/diy/https://www.0978.com.cn /https://www.jiaocheng.com/Installation method: Download template Unzip template Upload template Select template

How to upload local videos using Dedecms? Prepare the video file in a format that is supported by Dedecms. Log in to the Dedecms management backend and create a new video category. Upload video files on the video management page, fill in the relevant information and select the video category. To embed a video while editing an article, enter the file name of the uploaded video and adjust its dimensions.

Template replacement can be implemented in Dedecms through the following steps: modify the global.cfg file and set the required language pack. Modify the taglib.inc.php hook file and add support for language suffix template files. Create a new template file with a language suffix and modify the required content. Clear Dedecms cache.
