# Example client

1. <?php
2. /**
3. * Server controller
4. *
5. * @author Mark Nielsen
6. * @version $Id$
7. * @package blocks/helloworld
8. */
10. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
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;
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. }
25. #### DEMO CODE ####
26. // Setup Zend
27. mr_bootstrap::zend();
28. require_once 'Zend/Rest/Client.php';
30. // Lame-o
31. $https = str_replace('http', 'https', $CFG->wwwroot);
33. // Create a new HTTP client
34. $client = new Zend_Http_Client($https.'/blocks/helloworld/webservices.php');
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();
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 ####
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