1

I'm new to php and coding in general and I'm following this php-MediaWiki tutorial. I'm working with a Nitrous.io LAMP stack and as per the tutorial's instructions, I've installed the Zend framework to make use of its REST client. My Apache server seems to be working fine and when I use the Nitrous preview function to run the code sample below, the HTML search box appears as expected. However, absolutely nothing happens when I enter a search term and hit return. Obviously the php code is failing, but there are no error messages to help me diagnose the issue. I'm presuming that the problem lies with the Zend portions and I'm wondering if, in the four years since the tutorial was published, the Zend_Rest_Client functionality has been changed or deprecated. I looked at the documentation but as a noob, it's very hard for me to decipher where the problem could lie. I'd be really grateful if anyone could give me a pointer on what I should do to get this code working.

<html>
  <head></head>
  <body>
    <h2>Search</h2>
    <form method="post">
      Search: <input type="text" name="q" />
    </form>
    <?php
    // if form submitted
    if (isset($_POST['q'])) {
      // load Zend classes
      require_once 'Zend/Loader.php';
      Zend_Loader::loadClass('Zend_Rest_Client');

      try {
        // initialize REST client
        $wikipedia = new Zend_Rest_Client('http://en.wikipedia.org/w/api.php');
        // set query parameters
        $wikipedia->action('query');
        $wikipedia->list('search');
        $wikipedia->srwhat('text');
        $wikipedia->format('xml');
        $wikipedia->srsearch($_POST['q']);

        // perform request
        // iterate over XML result set
        $result = $wikipedia->get();
      } catch (Exception $e) {
      die('ERROR: ' . $e->getMessage());
      }
    ?>
    <h2>Search results for '<?php echo $_POST['q']; ?>'</h2>
    <ol>
    <?php foreach ($result->query->search->p as $r): ?>
      <li><a href="http://www.wikipedia.org/wiki/
      <?php echo $r['title']; ?>">
      <?php echo $r['title']; ?></a> <br/>
      <small><?php echo $r['snippet']; ?></small></li>
    <?php endforeach; ?>
    </ol>
    <?php 
    }
    ?>
  </body>
</html>
AticAtac
  • 11
  • 2

1 Answers1

0

I'm just posting this in case it might help someone else. In answer to my own question, yes, the Zend_Rest_Client component was removed from version 2 of the Zend framework, which is what is installed by default in Nitrous and PhpStorm. However, you can use Composer to install Zend version 1 alongside it and thereby make use of the Zend_Rest_Client. I also experienced trouble getting it to load but I eventually discovered this thread, which gave me the solution.

Community
  • 1
  • 1
AticAtac
  • 11
  • 2