1,105,625 Community Members

Help Getting an API Working

Member Avatar
tusharbhatia
Newbie Poster
17 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

I am trying to get a dictionary definition from the Wordnik API. I copied the code below from their example and it's not working. I've played around with it and I can't see what's wrong. I get this error: "Parse error: syntax error, unexpected $end on line 56".

If you could help me to get this working that would be wonderful. Thank you all for your time.

<?php 
  require("Wordnik.php"); 
  $api_key = "/*MY API KEY WAS HERE IT WAS REMOVED TO SAFEGUARD */";
  // 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 echo(urlencode($wotd)) ?>"><?php echo($wotd) ?></a></strong>!
      </h3>
      <p>
        <form method='get' action='/hello_dictionary.php'>
          Look up a word: 
          <input type='text' name='word' />
          <input type='submit' name='commit' value='Search' />
          <input type='submit' name='commit' value="I'm Feeling Wordie" />
        </form>
      </p>
<?php if (isset($wordstring)): ?>
  <hr />

  <!-- Definitions -->
  <div>
    <?php $definitions = $wordnik->getDefinitions($wordstring); ?>
    <h2>Definitions of <em><?php echo($wordstring) ?></em></h2>
    <?php if (empty($definitions)): ?>
      <em>Sorry, we couldn't find any definitions!</em>
    <?php else: ?>
      <ul>
        <?php 
          foreach($definitions as $definition) {
            if (isset($definition->text)) {
              echo("<li>".$definition->text."</li>");
            }
          }
        ?>
      </ul>
      <a href="http://wordnik.com/words/<?php echo(urlencode($wordstring)) ?>">more definitions</a>
    <?php endif ?>
  </div>
Member Avatar
urtrivedi
Posting Virtuoso
1,900 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 27 [?]
 
1
 

1) You are using : after if that is not valid
2) you must embed html in {} if it is conditional
3) endif is no php keywork rather you must use closing brace }

<?php 
  require("Wordnik.php"); 
  $api_key = "/*MY API KEY WAS HERE IT WAS REMOVED TO SAFEGUARD */";
  // 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 echo(urlencode($wotd)) ?>"><?php echo($wotd) ?></a></strong>!
      </h3>
      <p>
        <form method='get' action='/hello_dictionary.php'>
          Look up a word: 
          <input type='text' name='word' />
          <input type='submit' name='commit' value='Search' />
          <input type='submit' name='commit' value="I'm Feeling Wordie" />
        </form>
      </p>
<?php if (isset($wordstring)){ ?>
  <hr />

  <!-- Definitions -->
  <div>
    <?php $definitions = $wordnik->getDefinitions($wordstring); ?>
    <h2>Definitions of <em><?php echo($wordstring) ?></em></h2>
    <?php if (empty($definitions)) { ?>
      <em>Sorry, we couldn't find any definitions!</em>
    <?php }else{ ?>
      <ul>
        <?php 
          foreach($definitions as $definition) {
            if (isset($definition->text)) {
              echo("<li>".$definition->text."</li>");
            }
          }
        ?>
      </ul>
      <a href="http://wordnik.com/words/<?php echo(urlencode($wordstring)) ?>">more definitions</a>
    <?php } ?>
  </div>
  
  <?php } ?>
Member Avatar
tusharbhatia
Newbie Poster
17 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hmm.. I understand where I went wrong, but now I've got this problem:

"Fatal error: Maximum function nesting level of '100' reached, aborting!" It's on line 4 which is including "Wordnik.php". I understand what this error means, but I don't know what I should do about or how to edit the code around it. If I create another Wordnik.php file what should I put in it?

Can you help me there? But thanks for your explanation with the code - it helped me understand.

Member Avatar
tusharbhatia
Newbie Poster
17 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Never mind, I got it working with a little help of Wordnik support. Thanks.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: