API:Main page/zh


 * This is an overview of the "action" API. See the menu bar on the right for more detailed sub-topics and other APIs.

The MediaWiki action API is a web service that provides convenient access to wiki features, data, and meta-data over HTTP, via a URL usually at. Clients request particular "actions" by specifying an  parameter, mainly   to get information. It was known as  the  MediaWiki API, but MediaWiki now offers other web APIs such RESTBase and the Wikidata query service.

介绍
如果您是在寻找“内部 API”或者“PHP API”，查看 extension interface 这个页面，它允许PHP开发人员向维基百科增加新的功能.

The MediaWiki action API can be used to monitor a MediaWiki installation, or create a bot to automatically maintain one. Web API 提供了直接、高级别的方式来访问维基百科里的数据. 客户端程序可以自动登录维基、获取数据、通过 post 方式向 web service 发起 HTTP 请求以提交更改. 支持的客户端包括 机器人程序  、基于 web 的轻量级 JavaScript ，用于  导航的弹出窗口

新安装的MediaWiki中，此web服务默认启用，但管理员可以把它禁用.

對於MediaWiki有另外兩個朝外的接口：


 * Special:Export美元的出口頁面，它提供了批量出口的維基上的內容為XML. 閱讀 Export help article on meta.wikimedia.org以獲取更多信息.
 * 本標準的基於Web的界面（你很可能使用的是現在瀏覽這個頁面）. 閱讀參數的信息，使用基於Web的界面.

简单例子
这个URL让英文维基百科的web服务API向您传送首页的内容：

https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json

用任意编程语言向这个URL发送一个HTTP GET请求（或直接在浏览器中打开这个链接），您便可得到一个XML文档，其中包含“Main Page”页面中当前的wiki代码. 这就是一个能运作的web服务API. Changing the format to  will return a "pretty-printed" HTML result good for debugging.

我们来分解这个URL，以展示它如何工作.

接入点
這是''端點'. 這就像MediaWiki的Web服務API的主頁. 這個URL是英文維基百科的API的基礎URL，就像 網址是其網站的基本URL.

如果您写一个使用英文维基百科的程序，每一个您需要构造的URL都以这个基础URL开头. 如果您在其他MediaWiki系统上操作，您需要找到它的接入点，并相应地使用那个地址. 所有Wikimedia的wiki的接入点都符合这个模式：

Since, we have RSD discovery for the endpoint: look for the  in the HTML source of any page and extract the   URL; the actual link contains additional info. 例如，本維基是：

否則，沒有安全的方式找到任何維基端點. If you're lucky, either the full path to index.php will not be hidden under strange rewrite rules so that you'll only have to take the "edit" (or history) link and replace index.php (etc.) with api.php, or you'll be able to use the default script path (like ).

Now let's move on to the parameters in the query string of the URL.

格式
This tells the Wikimedia web service API that we want data to be returned in JSON format. You might also want to try  to get an HTML version of the result that is good for debugging. The API supports other output formats such as XML and native PHP, but there are plans to remove less popular formats (T95715), so you might not want to use them.

The action
The MediaWiki web service API implements dozens of actions and extensions implement many more; the dynamically generated API help documents all available actions on a wiki. In this case, we're using the "query" action to get some information. The "query" action is one of the API's most important actions, and it has extensive documentation of its own. What follows is just an explanation of a single example.

Action-specific parameters
The rest of the example URL contains parameters used by the "query" action. Here, we're telling the web service API that we want information about the Wiki page called "Main Page". (The %20 comes from percent-encoding a space.) If you need to work with multiple pages, please consider putting them all in one request to optimize network and server resources:. See the query documentation for details.

You can request many kinds of information, or properties, about a page. This parameter tells the web service API that we want information about a particular revision of the page. Since we're not specifying any revision information, the API will give us information about the latest revision &mdash; the main page of Wikipedia as it stands right now.

Finally, this parameter tells the web service API that we want the content of the latest revision of the page. If we passed in  instead, we'd get the latest page content and the name of the user who made the most recent revision.

Again, this is just one example. Queries are explained in more detail here, and the API reference lists all the possible actions, all the possible values for, and so on.



Getting started
Before you start using the MediaWiki web service API, be sure to read these documents:


 * The FAQ.
 * The page about input and output formats
 * The page about errors and warnings
 * Any policies that apply to the wiki you want to access, such as (for Wikimedia Foundation wikis) our terms of use. These terms apply to you when you access or edit using the web service API, just as they do when you use your web browser.

Beyond that point, what you need to read depends on what you want to do. The right-hand menu links to detailed, task-specific documentation, and some more general guidelines are given below.

Identifying your client
When you make HTTP requests to the MediaWiki web service API, be sure to specify a  header that properly identifies your client. Don't use the default  provided by your client library, but make up a custom header that identifies your script or service and provides some type of means of contacting you (e.g., an e-mail address).

An example User-Agent string might look like: MyCoolTool/1.1 (https://example.org/MyCoolTool/; MyCoolTool@example.org) BasedOnSuperLib/1.4

On Wikimedia wikis, if you don't supply a  header, or you supply an empty or generic one, your request will fail with an HTTP 403 error (cf. User-Agent policy). Other MediaWiki installations may have similar policies.

If you are calling the API from browser-based JavaScript, you won't be able to influence the  header: the browser will use its own. To work around this, use the  header:

In PHP, you can identify your user-agent with code such as this:

Or if you use cURL:

Logging in
Your client will probably need to log in to MediaWiki, possibly via its own user account. See the login manual page for details.

API etiquette
Please also read: API:Etiquette

If your requests obtain data that can be cached for a while, you should take steps to cache it, so you don't request the same data over and over again. More information about rate-limiting, concurrency, and general API etiquette can be found at API:Etiquette. Some clients may be able to cache data themselves, but for others (particularly JavaScript clients), this is not possible.

Per the HTTP specification, POST requests cannot be cached. Therefore, whenever you're reading data from the web service API, you should use GET requests, not POST.

Also note that a request cannot be served from cache unless the URL is exactly the same. If you make a request for, and cache the result, then a request for   will not go through the cache — even though MediaWiki returns the same data!

You should take care to normalize the URLs you send to the MediaWiki web service, so that slightly different user input won't cause you to waste time on unnecessary HTTP requests. You can normalize a list of page titles by removing duplicates and sorting the titles alphabetically. Similar techniques will work for other kinds of data.

Useful links
The menu bar on the right side of this page links to more detailed, task-specific documentation. Here are some links having to do with the API as a whole:
 * The API sandbox available on all Wikimedia wikis makes it easy to try out different actions interactively.
 * [//en.wikipedia.org/w/api.php The API reference] contains automatically-generated descriptions of all actions and parameters.
 * Hook into Wikipedia information using PHP and the MediaWiki API (IBM developerWorks article, 17 May 2011)
 * Hook into Wikipedia using Java and the MediaWiki API (6 April 2012)
 * The API tutorial leads you through hands-on exercises and includes a training video.
 * Mailing list for notifications and questions: API mailing list
 * Low-traffic mailing list for announcements only (all posts to this list are posted to mediawiki-api as well): mediawiki-api-announce
 * View and report API bugs in the MediaWiki-API Phabricator project (When reporting new bugs, don't forget to add MediaWiki-API to Projects)
 * — The current MediaWiki database schema
 * — The current MediaWiki database schema

Archived links

 * 2006 API discussion