Skip to content

Instantly share code, notes, and snippets.

@xosofox
Created April 29, 2010 11:44
Show Gist options
  • Save xosofox/383481 to your computer and use it in GitHub Desktop.
Save xosofox/383481 to your computer and use it in GitHub Desktop.
Retrieve Random Wikipedia Page Title
<?php
function randwik()
{
#retrieves a random page title from wikipedia
$ch = curl_init("http://de.wikipedia.org/wiki/Spezial:Random");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 GTB7.0 ( .NET CLR 3.5.30729)");
$content=curl_exec($ch);
$err=curl_error($ch);
curl_close($ch);
if ($err==0)
{
preg_match('/<h1.*?>(.*?)<\/h1>/',$content,$matches);
$randwik=$matches[1];
} else {
$randwik="";
}
return utf8_decode($randwik);
}
echo randwik();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment