API:Opensearch/Sample code 1
Appearance
From mediawiki.org
Python
[edit ]#!/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
[edit ]<?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
[edit ]/* 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 */ varurl="https://en.wikipedia.org/w/api.php"; varparams={ action:"opensearch", search:"Hampi", limit:"5", namespace:"0", format:"json" }; url=url+"?origin=*"; Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];}); fetch(url) .then(function(response){returnresponse.json();}) .then(function(response){console.log(response);}) .catch(function(error){console.log(error);});
MediaWiki JS
[edit ]/* 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 */ varparams={ action:'opensearch', search:'Hampi', limit:'5', namespace:'0', format:'json' }, api=newmw.Api(); api.get(params).done(function(data){ console.log(data); });