API:Opensearch
Appearance
Outdated translations are marked like this.
| Bu sayfa MediaWiki Eylem API'si belgelerinin bir parçasıdır. |
Vikide arama yapmak ve sonuçları OpenSearch biçiminde elde etmek için GET isteği. Not to be confused with the OpenSearch software suite.
API belgesi
Örnek
GET isteği
Yanıt
[
"Hampi",
[
"Hampi",
"Hampi (town)",
"Hampi Express",
...
],
[
"Hampi, also referred to as the Group of Monuments at Hampi, is a UNESCO World Heritage Site located in east-central Karnataka, India.",
"Hampi is a town in Hospet taluk of the Ballari district in the Indian state of Karnataka. Located along the Tungabhadra River in the east and center part of the state, near the border of Andhra Pradesh, Hampi is near the city of Hosapete.",
"The Hampi Express is a daily express train running between the Mysooru and Hubballi Junction, the headquarters of the South Western Railway in India.",
...
],
[
"https://en.wikipedia.org/wiki/Hampi",
"https://en.wikipedia.org/wiki/Hampi_(town)",
"https://en.wikipedia.org/wiki/Hampi_Express",
...
]
]
Wikimedia'da viki açıklamaları performans nedeniyle devre dışı bırakılır, bu nedenle ikinci dizi yalnızca boş dizeler içerir. T241437 sayfasına bakın.
Örnek kod
Python
#!/usr/bin/python3
"""
opensearch.py
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "opensearch",
"namespace": "0",
"search": "Hampi",
"limit": "5",
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
print(DATA)
PHP
<?php
/*
opensearch.php
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "opensearch",
"search" => "Hampi",
"limit" => "5",
"namespace" => "0",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
var_dump( $result );
JavaScript
/*
opensearch.js
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
*/
export async function getWikiSearch() {
const params = {
action: "opensearch",
search: "Hampi",
limit: "5",
namespace: "0",
format: "json",
};
const url =
"https://en.wikipedia.org/w/api.php?origin=*&" +
new URLSearchParams(params).toString();
try {
const data = await (await fetch(url)).json();
console.log(data);
} catch (error) {
console.error("Fetch error:", error);
}
}
MediaWiki JS
/*
opensearch.js
MediaWiki API Demos
Demo of `Opensearch` module: Search the wiki and obtain
results in an OpenSearch (http://www.opensearch.org) format
MIT License
*/
var params = {
action: 'opensearch',
search: 'Hampi',
limit: '5',
namespace: '0',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
console.log( data );
} );
Olası hatalar
| Kod | Bilgi |
|---|---|
| nosearch | search parametresi ayarlanmalıdır. |
| unknown_format | format parametresi için tanınmayan değer: aaa. |
Ek notlar
Bu API ile ilgili olarak, MediaWiki site hizmetlileri ve uzantı geliştiricileri için birkaç ek işaretçi:
- Extension:TitleKey - bu API'dan gelen arama önerilerinin büyük/küçük harfe duyarlı olmamasını sağlar.
LocalSettings.phpiçinde true olarak yapılandırılan Extension:TextExtracts ve$wgExtractsExtendOpenSearchXml, XML biçimindeki her bir öğenin maddeden bir metin ayıklamasıyla bir<Description>etiketi içermesine izin verir.
LocalSettings.phpiçinde true olarak yapılandırılan Extension:PageImages ve$wgPageImagesExpandOpenSearchXml, XML biçimindeki her öğenin makaleden küçük resim içeren bir<Image>etiketi içermesine izin verir.