API:Etiquette/zh

本页面教你使用 API 时都有哪些事情应当注意.

行为规范


请求限制
读取请求没有速率限制或者什么苛刻的条件，但是体谅一点，不要让站点访问速度变慢. 如果你威胁到站点的安全，多数管理员保留将你封禁的权力.

要一次一次地发送请求，而不是一次发送多个请求. 前一个请求完成了再发送下一个请求，这样请求速率会较安全. 如果你要操作多个项目，应该遵守以下条件：


 * 只要能用管道符号（ ）就不要给每个标题都创建一个新的请求. 例：
 * Using a instead of making a request for each result from another request.


 * 进行 API 调用时使用 GZip 压缩减少带宽消耗.

Requests which make edits, modify state or otherwise are not read-only requests, are subject to rate limiting. The exact rate limit being applied might depend on the type of action, your user rights and the configuration of the website you are making the request to. The limits that apply to you can be determined by accessing the action=query&meta=userinfo&uiprop=ratelimits API endpoint.

When you hit the request rate limit you will receive a with the error code. When you encounter this error, you may retry that request, however you should increase the time between subsequent requests. A common strategy for this is Exponential backoff.

Parsing of revisions
While it is possible to query for results from a specific revision number using the  parameter, this is an expensive operation for the servers. To retrieve a specific revision use the  parameter. For example:

The maxlag parameter
If your task is not interactive, i.e. a user is not waiting for the result, you should use the  parameter. The value of the  parameter should be an integer number of seconds. 例如：

This will prevent your task from running when the load on the servers is high. Higher values mean more aggressive behavior, lower values are nicer.

有关更多详细信息，请参见.

The User-Agent header
It is best practice to set a descriptive User Agent header. To do so, use. For example in PHP:

Do not simply copy the user-agent of a popular web browser. This ensures that if a problem does arise it is easy to track down where it originates.

If you are calling the API from browser-based JavaScript, you may not be able to influence the  header, depending on the browser. To work around this, use the  header.

See m:User-Agent_policy for more details.



数据类型
所有新的API用户. 有关更多详细信息，请参见.

性能
Downloading data in bulk is not always extremely efficient using the Action API. On Wikimedia wikis, there are faster ways to get data in bulk, see m:Research:Data and wikitech:Portal:Data Services for more details.

Other notes
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. 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.



参见

 * - 快速上手指南