Example client

  1. <?php
  2. /**
  3.  * Server controller
  4.  *
  5.  * @author Mark Nielsen
  6.  * @version $Id$
  7.  * @package blocks/helloworld
  8.  */
  9.  
  10. defined('MOODLE_INTERNAL'or die('Direct access to this script is forbidden.');
  11.  
  12. class block_helloworld_controller_server extends mr_controller_block {
  13.     /**
  14.      * Default screen
  15.      *
  16.      * Demo of mr_server_rest
  17.      */
  18.     public function view_action({
  19.         global $CFG$DB;
  20.  
  21.         if (!$DB->record_exists('user'array('username' => 'administrator'))) {
  22.             $this->notify->add_string('For the service to work correctly, please create a user with username/password = administrator');
  23.         }
  24.  
  25.         #### DEMO CODE ####
  26.         // Setup Zend
  27.         mr_bootstrap::zend();
  28.         require_once 'Zend/Rest/Client.php';
  29.  
  30.         // Lame-o
  31.         $https str_replace('http''https'$CFG->wwwroot);
  32.  
  33.         // Create a new HTTP client
  34.         $client  new Zend_Http_Client($https.'/blocks/helloworld/webservices.php');
  35.  
  36.         // Set the post parameters that we want
  37.         $client->setParameterPost(array(
  38.             'token'      => 'tokenvalue',      // Part of security requirements, pass a token
  39.             'wsusername' => 'administrator',   // Part of security requirements, pass username
  40.             'wspassword' => 'administrator',   // Part of security requirements, pass password
  41.             'method'     => 'test',            // The service method to execute
  42.             'say'        => 'Hello World!',    // The service parameter name
  43.         ));
  44.         // Send the request
  45.         $response $client->request('POST');
  46.         $request  $client->getLastRequest();
  47.  
  48.         // Dump the values
  49.         $output  $this->helper->dump($request'The sent request'true);
  50.         $output .= $this->helper->dump($response->getBody()'The returned response'true);
  51.         #### DEMO CODE ####
  52.  
  53.         return $this->output->heading('Demo of mr_server_rest').
  54.                $this->helper->highlight(__CLASS____FUNCTION__true).
  55.                $this->output->box($output'generalbox boxaligncenter boxwidthnormal');
  56.     }
  57. }

Documentation generated on Thu, 28 Jun 2012 16:33:51 -0700 by phpDocumentor 1.4.3