public static function instance($api_key) { if (self::$instance == NULL) { if (!isset($api_key) || $api_key == "YOUR_API_KEY" || trim($api_key) == '') { throw new Exception("You need to specify a valid api_key!"); } self::$instance = new Wordnik($api_key); } return self::$instance; }
<?php require "Wordnik.php"; $api_key = "YOUR_API_KEY"; // create an instance of the Wordnik class, to make api calls $wordnik = Wordnik::instance($api_key); // get the Word of the Day $wotd = $wordnik->getWordOfTheDay(); $wotd = $wotd->wordstring; // if the user pressed the "I'm Feeling Wordie" button, fetch a random word and set it to $wordstring if (isset($_GET['commit']) && $_GET['commit'] == "I\\'m Feeling Wordie") { $word = $wordnik->getRandomWord(); $wordstring = $word->wordstring; } else { if (isset($_GET['word'])) { // the user is searching for a specific word, either via the form or a link $wordstring = $_GET['word']; } } ?> <html> <head> <title>Hello, Dictionary!</title> </head> <body> <div> <h1>Hello, Dictionary!</h3> <h3>The Wordnik Word of the Day for <?php echo date("l F j, Y"); ?> is... <strong><a href="/hello_dictionary.php?word=<?php
/** * Set the API key for this Wordnik instance * * @param String $key */ public function setApiKey($key) { self::$api_key = $key; }
$xmlObj = simplexml_load_string($response); $arrXml = objectsIntoArray($xmlObj); //print_r($arrXml); //echo count($arrXml['definitions']); if (count($arrXml) > 0) { if (isset($arrXml['definition']['text'])) { echo $arrXml['definition']['text']; } else { foreach ($arrXml['definition'] as $i => $definition) { echo $i + 1 . ". " . $definition['text'] . "\n"; } } } } } $wordnik = new Wordnik($_GET['q']); $wordnik->spellSuggest(); function objectsIntoArray($arrObjData, $arrSkipIndices = array()) { $arrData = array(); if (is_object($arrObjData)) { $arrObjData = get_object_vars($arrObjData); } if (is_array($arrObjData)) { foreach ($arrObjData as $index => $value) { if (is_object($value) || is_array($value)) { $value = objectsIntoArray($value, $arrSkipIndices); // recursive call } if (in_array($index, $arrSkipIndices)) { continue;
/** * An example of how to populate a new sfWordnikWord object using it's populate method. * * @param sfWebRequest $request */ public function executeNewWord(sfWebRequest $request) { $word = new sfWordnikWord(); // NOTE -- changed this from `getGetParameter` to `getParameter.` (andyinabox - 2010-05-20) $word->populate(Wordnik::getWordObject($request->getParameter("word"))); // $word->name = $request->getGetParameter("word"); // $word->Definitions[]->text = "To see if something works."; // $word->Examples[]->text = "Let's test this motor, see if it works.'"; // $word->RelatedWords[]->text = "try"; $word->save(); $this->success = "Awesome."; }