Getting a random Wikipedia article using PHP + cURL

The following code gets a completely random page from Wikipedia and returns some HTML code with a link to the article. Visiting in your browser will automatically redirect to a random article and the code takes in to account the redirect and gives you the information on the last page in the redirect.


function get_random_wikipedia_article() {
  if (!
function_exists('curl_init')) return FALSE;
$url '';
$options = array(
CURLOPT_RETURNTRANSFER => true,     // return web page
CURLOPT_HEADER         => false,    // don't return headers
CURLOPT_FOLLOWLOCATION => true,     // follow redirects
CURLOPT_ENCODING       => "",       // handle all encodings
CURLOPT_USERAGENT      => "spider"// who am i
CURLOPT_AUTOREFERER    => true,     // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 30,      // timeout on connect
CURLOPT_TIMEOUT        => 30,      // timeout on response
CURLOPT_MAXREDIRS      => 3,       // stop after 10 redirects

  $ch curl_init($url);
$content curl_exec($ch);
$err curl_errno($ch);
$errmsg curl_error($ch);
$header curl_getinfo($ch);

  if (preg_match('/<title>(.*?)<\/title>/i'$content$matches)) {
$title str_replace(' - Wikipedia, the free encyclopedia'''$matches[1]);

  return '<a href="'$header['url'] .'">'$title .'</a>';


works perfectly!! thank you!!! :)

Perfect, Thanks :), How can we write in wikipedia via curl ?