Home CMS Tutorial DEDECMS How dedecms writes API interface

How dedecms writes API interface

Jul 31, 2019 am 10:55 AM
dedecms json

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:

1

2

3

{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:

1

2

3

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:

1

2

3

4

5

6

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.

1

2

3

4

5

6

[

 {"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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?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:

1

2

3

{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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

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.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

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

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

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.

Where is the imperial cms resource network template? Where is the imperial cms resource network template? Apr 17, 2024 am 10:00 AM

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 use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

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.

How dedecms implements template replacement How dedecms implements template replacement Apr 16, 2024 pm 12:12 PM

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.

How to upload local videos to dedecms How to upload local videos to dedecms Apr 16, 2024 pm 12:39 PM

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.

What website can dedecms do? What website can dedecms do? Apr 16, 2024 pm 12:24 PM

Dedecms is an open source CMS that can be used to create various types of websites, including: news websites, blogs, e-commerce websites, forums and community websites, educational websites, portals, other types of websites (such as corporate websites, personal websites, photo album websites, video sharing website)

See all articles