{"id":765,"date":"2009-10-29T08:12:32","date_gmt":"2009-10-29T13:12:32","guid":{"rendered":"http:\/\/itp.indiamos.com\/blog\/?p=765"},"modified":"2017-12-28T03:12:50","modified_gmt":"2017-12-28T08:12:50","slug":"product-of-a-restful-night","status":"publish","type":"post","link":"https:\/\/itp.indiamos.com\/blog\/2009\/10\/29\/product-of-a-restful-night\/","title":{"rendered":"Product of a RESTful night"},"content":{"rendered":"<p><a href=\"http:\/\/www.flickr.com\/photos\/betsssssy\/466167256\/\"><img loading=\"lazy\" src=\"https:\/\/i1.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/reading_in_bed.jpg?resize=474%2C356\" alt=\"reading in bed\" title=\"reading in bed\" width=\"474\" height=\"356\" class=\"alignnone size-full wp-image-770\" srcset=\"https:\/\/i1.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/reading_in_bed.jpg?w=500&amp;ssl=1 500w, https:\/\/i1.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/reading_in_bed.jpg?w=400&amp;ssl=1 400w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>For this week&#8217;s homework in <a href=\"http:\/\/itp.nyu.edu\/understandingnetworks\/Main\/Syllabus\">Understanding Networks<\/a>, our assignment was as follows:<\/p>\n<blockquote><p>REST assignment. Communication in not-so-realtime. Deliver information to the user through the browser from somewhere else. Update the information they&#8217;re seeing while they interact, preferably based on input from multiple sources.<\/p><\/blockquote>\n<p><a href=\"http:\/\/itp.indiamos.com\/blog\/2009\/10\/22\/the-project-that-would-not-die\/\">As I mentioned last week<\/a>, I wanted to work with the newly released <a href=\"http:\/\/docs.wordnik.com\/api\/\">Wordnik API<\/a>. <!--more--> I thought it would be fun to have a Web page that would let you <a href=\"http:\/\/en.wikipedia.org\/wiki\/Representational_State_Transfer\" title=\"Representational State Transfer\" rel=\"wikipedia\">RESTfully<\/a> 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.<\/p>\n<p>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&#8217;s worth of work to having the folly to trust BBEdit&#8217;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&#8217;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: <a href=\"http:\/\/itp.nyu.edu\/%7Eia303\/networks\/wordhopper\/\">Wordhopper<\/a>.<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/indiamos\/4054917815\/\" title=\"Wordhopper - ITPindia by indiamos, on Flickr\"><img loading=\"lazy\" src=\"https:\/\/i1.wp.com\/farm3.static.flickr.com\/2705\/4054917815_dff854a96f.jpg?resize=474%2C492\" alt=\"Wordhopper - ITPindia\" width=\"474\" height=\"492\" data-recalc-dims=\"1\" \/><\/a><br \/>\n<a href=\"http:\/\/itp.nyu.edu\/%7Eia303\/networks\/wordhopper\/\">http:\/\/itp.nyu.edu\/~ia303\/networks\/wordhopper\/<\/a><\/p>\n<p>So, if you go to the base URL, you&#8217;ll see the current word of the day from Wordnik, followed by a definition and a set of usage examples.<\/p>\n<p>If you tack a word onto the end of that URL, you get the word and just its usage examples.<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/indiamos\/4055667774\/\" title=\"Wordhopper - ITPindia by indiamos, on Flickr\"><img loading=\"lazy\" src=\"https:\/\/i1.wp.com\/farm3.static.flickr.com\/2505\/4055667774_2eeab6c6f0.jpg?resize=474%2C380\" alt=\"Wordhopper - ITPindia\" width=\"474\" height=\"380\" data-recalc-dims=\"1\" \/><\/a><br \/>\n<a href=\"http:\/\/itp.nyu.edu\/%7Eia303\/networks\/wordhopper\/gastropod\">http:\/\/itp.nyu.edu\/~ia303\/networks\/wordhopper\/gastropod<\/a><\/p>\n<p>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.<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/indiamos\/4055676722\/\" title=\"Wordhopper - ITPindia by indiamos, on Flickr\"><img loading=\"lazy\" src=\"https:\/\/i1.wp.com\/farm3.static.flickr.com\/2658\/4055676722_d5645d493c.jpg?resize=409%2C500\" alt=\"Wordhopper - ITPindia\" width=\"409\" height=\"500\" data-recalc-dims=\"1\" \/><\/a><br \/>\n<a href=\"http:\/\/itp.nyu.edu\/%7Eia303\/networks\/wordhopper\/monster\/d\">http:\/\/itp.nyu.edu\/~ia303\/networks\/wordhopper\/monster\/d<\/a><\/p>\n<p>Ta-da.<\/p>\n<p>The PHP looks like this:<\/p>\n<p>[PHP]<br \/>\n<!DOCTYPE html><br \/>\n<html><br \/>\n<head><br \/>\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/><br \/>\n<title>Wordhopper &#8211; ITPindia<\/title>\n<link rel=\"stylesheet\" href=\"http:\/\/itp.nyu.edu\/~ia303\/networks\/wordhopper\/wordhopper.css\" type=\"text\/css\">\n<\/head><br \/>\n<body><\/p>\n<div id=\"main\">\n<h1>Wordhopper<\/h1>\n<p><?php\n\/\/ Get Wordnik API key\ninclude \"wordnikauth.php\";\n\n\/\/ initialize variables:\n$baseurl = \"http:\/\/api.wordnik.com\/api\/word.xml\/\";\n$word = \"\";\n\n\/\/ Check whether a word has been passed in. If not, we'll pull in the WOTD as a starting point.\n\n\/\/ split the incoming URI string into tokens:\n$tokens = explode(\"\/\", $_SERVER['REQUEST_URI']);\n\n\/\/ if you got four tokens, you've received a word.\nif (count($tokens) > 4) {<br \/>\n\t$word = $tokens[4];<br \/>\n\tif ($word != &#8220;&#8221;) {<br \/>\n\t\techo &#8220;<\/p>\n<h2>Your Word<\/h2>\n<p>&#8220;;<br \/>\n\t\techo &#8220;<\/p>\n<p>&#8221; . $word . &#8220;<\/p>\n<p>&#8220;;<br \/>\n\t}<br \/>\n}<\/p>\n<p>\/\/ If no word was inserted, look up the word of the day.<br \/>\nif ($word == &#8220;&#8221; ) {<br \/>\n\t$wotd_request_url = &#8220;http:\/\/api.wordnik.com\/api\/wordoftheday.xml?api_key=&#8221; . $api_key;<br \/>\n\t$wotdfile = file_get_contents($wotd_request_url);<br \/>\n\t$wotdxmldata = simplexml_load_string($wotdfile);<br \/>\n\t$word = $wotdxmldata->word;<br \/>\n\t$wotddef = $wotdxmldata->definition->text;<br \/>\n\techo &#8220;<\/p>\n<h2>Wordnik.com Word of the Day<\/h2>\n<p>&#8220;;<br \/>\n\techo &#8220;<\/p>\n<p>&#8221; . $word . &#8220;<\/p>\n<p>&#8220;;<br \/>\n\techo &#8216;<\/p>\n<h3>Definition<\/h3>\n<p>&#8216;;<br \/>\n\techo &#8216;<\/p>\n<p>&#8216; . $wotddef . &#8216;<\/p>\n<p>&#8216;;<br \/>\n}<\/p>\n<p>\/\/ if you got five tokens, you&#8217;ve received some other command.<br \/>\nif (count($tokens) > 5) {<br \/>\n\t$details = $tokens[5];<\/p>\n<p>\t\/\/ Does token 5 contain the letter d?<br \/>\n\t$pos = strpos($details, &#8220;d&#8221;);<br \/>\n\tif ($pos === false) {<br \/>\n\t\t\/\/ It wasn&#8217;t a d, so we&#8217;ll ignore it for now. Later we may add some other options.<br \/>\n\t} else {<br \/>\n\t\t\/\/ d stands for definition, so let&#8217;s look that up.<br \/>\n\t\t$def_request_url = &#8216;http:\/\/api.wordnik.com\/api\/word.xml\/&#8217; . $word . &#8216;\/definitions?api_key=&#8217; . $api_key;<br \/>\n\t\t$deffile = file_get_contents($def_request_url);<br \/>\n\t\t$defxmldata = simplexml_load_string($deffile);<br \/>\n\t\techo &#8216;<\/p>\n<h3>Definitions<\/h3>\n<p>&#8216;;<\/p>\n<p>\t\t\/\/ Display definitions for the word.<br \/>\n\t\tforeach ($defxmldata->definition as $definition) {<br \/>\n\t\t\t$headword = $definition->headword;<br \/>\n\t\t\t$partOfSpeech = $definition->partOfSpeech;<br \/>\n\t\t\t$defTxtSummary = $definition->defTxtSummary;<br \/>\n\t\t\t$defTxtExtended = $definition->defTxtExtended;<br \/>\n\t\t\tif ($headword != $word) {<br \/>\n\t\t\t\techo &#8216;<\/p>\n<h4>&#8216; . $headword . &#8216;<\/h4>\n<p>&#8216;;<br \/>\n\t\t\t}<br \/>\n\t\t\techo &#8216;<\/p>\n<p class=\"definition\"><span class=\"pos\">&#8216; . $partOfSpeech . &#8216;<\/span> &#8216; . $defTxtSummary . &#8216;<br \/>&#8216; . $defTxtExtended . &#8216;<\/p>\n<p>&#8216;;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<\/p>\n<p>\/\/ Build a request to look up a word&#8217;s examples.<br \/>\n$request_url = $baseurl . $word . &#8220;\/examples?api_key=&#8221; . $api_key;<\/p>\n<p>$file = file_get_contents($request_url);<br \/>\n$xmldata = simplexml_load_string($file);<\/p>\n<p>echo &#8216;<\/p>\n<h3>Usage Examples<\/h3>\n<p>&#8216;;<\/p>\n<p>\/\/ Display usage examples for the word.<br \/>\nforeach ($xmldata->example as $example) {<br \/>\n\t$quote = $example->display;<br \/>\n\t$source = $example->title;<br \/>\n\t$url = $example->url;<br \/>\n\t$year = $example->year;<br \/>\n\techo &#8216;<\/p>\n<blockquote><p>&#8216; . $quote . &#8216;<\/p><\/blockquote>\n<p>&#8216;;<br \/>\n\techo &#8216;<\/p>\n<p class=\"source\">&mdash;<a href=\"' . $url . '\">&#8216; . $source . &#8216;<\/a>, &#8216; . $year . &#8216;<\/p>\n<p>&#8216;;<br \/>\n}<\/p>\n<p>?>\n<\/p><\/div>\n<p><\/body><br \/>\n<\/html><br \/>\n[\/php]<\/p>\n<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 <a href=\"http:\/\/itp.indiamos.com\/blog\/category\/bookalator\/\">Bookalator<\/a>, as the semester rolls on.<\/p>\n<p><span style=\"color: gray; font-size: smaller;\">Photo: <a href=\"http:\/\/www.flickr.com\/photos\/betsssssy\/466167256\/\">61\/365: I try to read before sleeping every night.<\/a> by Betsssssy  \/ Betsy Fletcher; <a href=\"http:\/\/creativecommons.org\/licenses\/by\/2.0\/deed.en\">some rights reserved<\/a>.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this week&#8217;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&#8217;re seeing while they interact, preferably based on input from multiple sources. As I mentioned last week, I wanted to work with the newly &hellip; <a href=\"https:\/\/itp.indiamos.com\/blog\/2009\/10\/29\/product-of-a-restful-night\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Product of a RESTful night<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":770,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,22],"tags":[],"jetpack_featured_media_url":"https:\/\/i1.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/reading_in_bed.jpg?fit=500%2C375&ssl=1","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3qY10-cl","_links":{"self":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/765"}],"collection":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/comments?post=765"}],"version-history":[{"count":9,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":945,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/765\/revisions\/945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/media\/770"}],"wp:attachment":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/categories?post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/tags?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}