a woman reading in bed

Product of a RESTful night

reading in bed

For this week’s homework in Understanding Networks, our assignment was as follows:

REST assignment. Communication in not-so-realtime. Deliver information to the user through the browser from somewhere else. Update the information they’re seeing while they interact, preferably based on input from multiple sources.

As I mentioned last week, I wanted to work with the newly released Wordnik API. I thought it would be fun to have a Web page that would let you RESTfully look up a word, for which it would return usage examples from Wordnik. And each word in the examples would be linked to the example set for itself, so you could click through endlessly.

And then I thought, given my habit of biting off more than I can chew, homeworkwise, that it would be a better plan to just do the minimum amount of programming required for the assignment, and then, if I had time left over after debugging, I could fancy it up. After I had lost (a) an hour’s worth of work to having the folly to trust BBEdit’s clipboard history; (b) at least two more hours to trying to route around what I thought was an authentication issue but that was, in fact, just me-not-understanding-the-example-code issue; and (c) maybe another hour trying to fix a genuine permissions problem, which I gave up on but will have to solve later, finishing even the most basic version of this toy seemed unlikely. I like PHP, though, and I find the documentation reasonably accessible, so I don’t get as discouraged as easily in it as I do with Java. So after a million or so edit-save-refresh cycles, I finally came up with this silly thing: Wordhopper.

Wordhopper - ITPindia

So, if you go to the base URL, you’ll see the current word of the day from Wordnik, followed by a definition and a set of usage examples.

If you tack a word onto the end of that URL, you get the word and just its usage examples.

Wordhopper - ITPindia

And, finally, if you add a /d to the end of the URL, after your personally chosen word, you get all the available definitions for that word.

Wordhopper - ITPindia


The PHP looks like this:

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Wordhopper - ITPindia</title>
<link rel="stylesheet" href="http://itp.nyu.edu/~ia303/networks/wordhopper/wordhopper.css" type="text/css">
<div id="main">
// Get Wordnik API key
include "wordnikauth.php";

// initialize variables:
$baseurl = "http://api.wordnik.com/api/word.xml/";
$word = "";

// Check whether a word has been passed in. If not, we'll pull in the WOTD as a starting point.

// split the incoming URI string into tokens:
$tokens = explode("/", $_SERVER['REQUEST_URI']);

// if you got four tokens, you've received a word.
if (count($tokens) > 4) {
	$word = $tokens[4];
	if ($word != "") {
		echo "<h2>Your Word</h2>";
		echo "<p>" . $word . "</p>";

// If no word was inserted, look up the word of the day.
if ($word == "" ) {
	$wotd_request_url = "http://api.wordnik.com/api/wordoftheday.xml?api_key=" . $api_key;
	$wotdfile = file_get_contents($wotd_request_url);
	$wotdxmldata = simplexml_load_string($wotdfile);
	$word = $wotdxmldata->word;
	$wotddef = $wotdxmldata->definition->text;
	echo "<h2>Wordnik.com Word of the Day</h2>";
	echo "<p>" . $word . "</p>";
	echo '<h3>Definition</h3>';
	echo '<p>' . $wotddef . '</p>';

// if you got five tokens, you've received some other command.
if (count($tokens) > 5) {
	$details = $tokens[5];

	// Does token 5 contain the letter d?
	$pos = strpos($details, "d");
	if ($pos === false) {
		// It wasn't a d, so we'll ignore it for now. Later we may add some other options.
	} else {
		// d stands for definition, so let's look that up.
		$def_request_url = 'http://api.wordnik.com/api/word.xml/' . $word . '/definitions?api_key=' . $api_key;
		$deffile = file_get_contents($def_request_url);
		$defxmldata = simplexml_load_string($deffile);
		echo '<h3>Definitions</h3>';

		// Display definitions for the word.
		foreach ($defxmldata->definition as $definition) {
			$headword = $definition->headword;
			$partOfSpeech = $definition->partOfSpeech;
			$defTxtSummary = $definition->defTxtSummary;
			$defTxtExtended = $definition->defTxtExtended;
			if ($headword != $word) {
				echo '<h4>' . $headword . '</h4>';
			echo '<p class="definition"><span class="pos">' . $partOfSpeech . '</span> ' . $defTxtSummary . '<br />' . $defTxtExtended . '</p>';

// Build a request to look up a word's examples.
$request_url = $baseurl . $word . "/examples?api_key=" . $api_key;

$file = file_get_contents($request_url);
$xmldata = simplexml_load_string($file);

echo '<h3>Usage Examples</h3>';

// Display usage examples for the word.
foreach ($xmldata->example as $example) {
	$quote = $example->display;
	$source = $example->title;
	$url = $example->url;
	$year = $example->year;
	echo '<blockquote>' . $quote . '</blockquote>';
	echo '<p class="source">—<a href="' . $url . '">' . $source . '</a>, ' . $year . '</p>';


So, I learned a lot from doing this, which was the goal, and I feel more optimistic about finding some way to tie Wordnik data into the Bookalator, as the semester rolls on.

Photo: 61/365: I try to read before sleeping every night. by Betsssssy / Betsy Fletcher; some rights reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.