How To Display WikiText
I would like to fetch the WikiText from a Wikipedia page and display it in a browser using Javascript. The following is what I currently have:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<div id="wikiInfo"> </div>
<script>
$.get('http://en.wikipedia.org/w/index.php?action=raw&title=Dog&callback=?',
function(data) {
$("div").add(data);
alert('load performed');
});
</script>
</body>
</html>
However, this doesn't seem to work. Eventually, I would like to be able to save the fetched wikitext开发者_C百科 to a variable as well, so any help on that would be appreciated too.
It is cross domain, so it doesn't work... make a wiki.php and fetch "wiki.php?title=Dog" with JS...
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close ($ch);
return curl_exec($ch);
}
$title = $_GET["title"];
echo curl("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&callback=?&titles=$title");
Edited using @Tgr WikiPedia link. I also suggest parsing JSON in PHP and then outputing HTML or raw text.
action=raw
does not accept JSONP callback parameters. You should do proper API calls:
$.getJSON('http://en.wikipedia.org/w/api.php?'+
'action=query&prop=revisions&rvprop=content'+
'&format=json&callback=?&titles=Dog',
function(result) { /* process JSON result */ });
See API documentation (also this remark about JSONP restrictions), a working example and the Wikipedia articles about same-origin policy and JSONP to understand why your first approach didn't work.
You can study the format of the API results by replacing 'json' with 'jsonfm': http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=jsonfm&titles=Dog
精彩评论