Skip to content

Instantly share code, notes, and snippets.

@johndbritton
Created January 14, 2011 23:31
Show Gist options
  • Save johndbritton/780502 to your computer and use it in GitHub Desktop.
Save johndbritton/780502 to your computer and use it in GitHub Desktop.
Wikipedia via SMS
<?php
header('Content-type: text/xml');
$url = "http://en.wikipedia.org/w/api.php?action=parse&format=xml&prop=sections&page=".$_POST['Body'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Textipedia");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXMLElement($response);
$output = "";
if(count($doc->parse->sections->s) > 0) {
foreach($doc->parse->sections->s as $section) {
if($section[toclevel] == 1) {
$output .= "\n ".$section[line];
}
}
}
if ($output == "") {
$output = "Article not found: Text the title of a Wikipedia article to get an outline. Try 'Universe'.";
}
?>
<Response>
<Sms><?php echo substr($output, 0, 160); ?></Sms>
</Response>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment