When using Anq CMS to manage website content, we often need to retrieve detailed information about specific documents from the backend, whether it is to display content on custom pages or integrate data into other systems (such as mini-programs, mobile applications, etc.).AnQi CMS provides a powerful and flexible API interface, one of the core features of which is to obtain specific document details through the document ID.
Today, let's delve into how to use the document ID to easily obtain the document details of AnQi CMS, making your content operation more efficient and automated.
Core steps: How to obtain document details through the API
To obtain the document details of Anqi CMS, you need to use the "Get Document Details Interface" provided by it.This is like giving CMS a house number (document ID), and it will tell you all the public information in that house (document).
API call address and request method:
You can access this interface through the following address:{域名地址}/api/archive/detail
Please note,{域名地址}Replace with your actual website domain, for examplehttps://www.yourdomain.com/api/archive/detail.
The calling method of this interface isGETRequest, meaning you can access it directly through the browser or request data using the HTTP GET method in your code.
Request parameters: ID and alias, flexible selection.
To specify the details of the document you want to retrieve, you need to provide a parameter. Anqi CMS provides two very flexible options in this regard:
- id (int type, required):This is the unique numeric identifier of the document in the system. If you know the document ID, you can enter it directly.
- filename (string type, optional):If you have entered
id, you do not need to pass it againfilenameBut if you do not know the document ID, but know the document URL alias (usually the friendly name in the document link), you can also access it throughfilenameTo get the document details. This is very useful in many scenarios, for example, if you only know the short link of a news article.
Remember,idandfilenameYou only need to choose one, entering either one will successfully retrieve the information.
Sample data request:
Suppose you want to get the details of the document with ID:1The request would look like this:{域名地址}/api/archive/detail?id=1
If you want to use an aliasanqicms-tutorialTo get the document, the request would be:{域名地址}/api/archive/detail?filename=anqicms-tutorial
Understand the data returned by the API
After you successfully call the interface, Anqi CMS will return a JSON formatted data packet containing all the relevant information of the document you requested. This data packet usually includes the following core parts:
code(integer type):Error code. Usually0Indicates a successful request, other non-zero values indicate an error occurred.msg(string type):Error reason description. Ifcodeis not0Here will provide specific error information to help you troubleshoot.data(object type):This is the core content of the document details. If the request is successful, this will be an object containing various properties of the document; if the document does not exist or an error occurs, this field may benull.
dataObject: the treasure trove of document information
dataThe object contains all aspects of the document, from which you can extract the information you need:
Basic information of the document:
id: The unique ID of the document.title: The document title.description: Document description.url_token: The URL alias of the document.keywords: Document keywords.
Rich content presentation:
data.content: This is the actual content details of the document, usually returned as an HTML string, for direct rendering on the page.logo: The document's logo image address.thumb: The document's thumbnail image address.images: A string array containing the document's group image (multiple images) addresses.
Important operational metadata:
seo_title: The SEO title of the document, used for search engine optimization.views: Document's view count.comment_count: The number of comments in the document.created_time: Document release timestamp.updated_time: The last update timestamp of the document.flag: The recommended attributes of the document, such as headline, recommendation, slideshow, etc.
: Associated information and custom extensions.
categoryThis is a nested object that contains detailed information about the document's category, such as category ID, category name, category alias, etc., allowing you to easily obtain the category context.extraThis is a very powerful field, which will display according to the custom field you set in the Anqicms backend model. For example, if you add an 'author' field to the article model, then this will include the author's information, the structure is usuallykey => itemof whichnameIs the field name,valueIs the value entered.
Return data example (simplified):
{
"code": 0,
"data": {
"id": 1,
"title": "欢迎使用AnqiCMS",
"seo_title": "",
"url_token": "anqicms",
"keywords": "",
"description": "欢迎使用AnqiCMS",
"views": 1338,
"logo": "https://en.anqicms.com/uploads/202012/7/bd36c37ef742c7be.webp",
"thumb": "https://en.anqicms.com/uploads/202012/7/thumb_bd36c37ef742c7be.webp",
"created_time": 1607308159,
"category": {
"id": 1,
"title": "AnqiCMS帮助",
"url_token": "goruning",
"content": "<p>欢迎使用AnqiCMS</p>"
},
"data": {
"content": "<p>欢迎使用AnqiCMS</p>"
},
"extra": {
"author": {
"name": "作者",
"value": "AnqiCMS",
"default": ""
}
}
},
"msg": ""
}
Application scenarios in practice
Obtaining document details through document ID is one of the cornerstones of Anqi CMS API, and its application scenarios are very extensive:
- Build highly customized detail pages:You can fully control the way documents are displayed on the front end, no longer limited by preset templates.
- Develop cross-platform applications:Whether it is PC terminal, H5, mini-program or native APP, unified document data source can be obtained through this interface.
- SEO optimization:Obtain
seo_title/keywordsanddescriptionInformation, can be dynamically set on the front-end page SEO metadata. - Data analysis and content recommendation:Combine
views/comment_countData, you can build a more intelligent content recommendation system or perform data analysis. - Content aggregation and distribution:Quickly obtain detailed information about a large number of documents, used to build content aggregation platforms or distribute content to third-party platforms.
Tips for error handling
When using the API, understanding common error codes can help you locate problems more quickly:
code: 0: Everything is normal, the data is in:datain the field.code: -1: General error, the specific reason will be displayed in:msgField contains, for example, 'Document does not exist'.code: 1001: Not logged in. If your CMS is set to require login to access documents, and you have not provided valid credentials, this error will occur.code: 1002: Unauthorized. Even if you are logged in, you may not have sufficient permissions to access certain documents.
By understanding and making good use of this document detail interface, you can fully utilize the content management capabilities of Anqi CMS, bringing more flexible and powerful content display and interactive experience to your website or application.
Frequently Asked Questions (FAQ)
Q1: If I entered multipleidandfilename, which parameter will the system use first?
A1: According to the documentation of Anqi CMS, if you enter multipleidandfilename, the system will usually prioritize processingidParameter. To avoid confusion and ensure the clarity of the request, it is recommended to pass only one of the parameters, choosing the one that is most convenient or most explicit in your hand.
Q2: If the document I request does not exist, what will the API return?
A2: If the document ID or filename you request does not exist, the API will return acodeWith-1The status code, andmsgfield provides similar prompts like 'Document not found'. At this point,datafield is usuallynullor an empty object. In your program, you should handlecodeanddataField judgment is carried out to properly handle the situation where the document does not exist.
Q3: Can I get only the title and summary of the document through this interface, rather than all fields?
A3: The “Get Document Details Interface” is designed to return all available details of the document in one go.This means you cannot directly control the parameter to only return a part of the fields.If you only need the title, abstract, and other limited information of the document, without the full document content, you can get the complete data in front-end or back-end and then extract or filter the required fields according to your needs for display.If performance becomes a bottleneck, and you need a small amount of information from a large number of documents, then the "Get Document List Interface" (/api/archive/list)May be more suitable,“