PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/rest_client.php

https://bitbucket.org/_azhar/codeigniter-rest-example
PHP | 133 lines | 75 code | 16 blank | 42 comment | 4 complexity | 58215a0060d1bf6c310b3f9b54a35682 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. // Created on Aug 26, 2011 by Damiano Venturin @ Squadra Informatica
  3. class Rest_Client extends CI_Controller {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. // Load the configuration file
  8. $this->load->config('rest');
  9. // Load the rest client
  10. $this->load->spark('restclient/2.0.0');
  11. $this->rest->initialize(array('server' => $this->config->item('rest_server')));
  12. //$this->rest->initialize(array('server' => 'http://localhost/codeigniter-rest-example/index.php/rest_server/'));
  13. }
  14. public function index()
  15. {
  16. $data['methods_list'] = $this->displayAPI();
  17. $this->load->view('restDoc',$data);
  18. }
  19. /**
  20. *
  21. * Produces a human readable list of methods available on the server side
  22. */
  23. public function displayAPI()
  24. {
  25. //get methods list
  26. $methods_html = '';
  27. $methods = (array) $this->rest->get('API/format/json');
  28. //show the docstring for the method
  29. if(count($methods['functions'])>0)
  30. {
  31. foreach ( $methods['functions'] as $method) {
  32. if(empty($method->docstring))
  33. {
  34. $methods_html .= '<dt>'.$method->function.'</dt><dd>No description available</dd>';
  35. } else {
  36. $methods_html .= '<dt>'.$method->function.'</dt><dd>'.$method->docstring.'</dd>';
  37. }
  38. }
  39. $methods_html .= '</dl>';
  40. }
  41. // echo '<pre>';
  42. // print_r($methods);
  43. // echo '</pre>';
  44. return $methods_html;
  45. }
  46. /**
  47. *
  48. * Access API "users" using GET through Phil's rest client
  49. */
  50. public function getUsers()
  51. {
  52. echo '<pre>';
  53. print_r($this->rest->get('users/format/json'));
  54. echo '</pre>';
  55. }
  56. /**
  57. *
  58. * Access API "users" using GET through json_decode
  59. */
  60. public function getUsersJson()
  61. {
  62. echo '<pre>';
  63. print_r(json_decode(file_get_contents($this->config->item('rest_server').'users/format/json')));
  64. echo '</pre>';
  65. }
  66. /**
  67. *
  68. * Access API "user" using GET through Phil's rest client
  69. */
  70. public function getUser()
  71. {
  72. echo '<pre>';
  73. print_r($this->rest->get('user/id/1/format/json'));
  74. echo '</pre>';
  75. }
  76. /**
  77. *
  78. * Access API "user" using DELETE through Phil's rest client
  79. */
  80. public function delUser()
  81. {
  82. echo '<pre>';
  83. print_r($this->rest->delete('user/id/2/format/json'));
  84. echo '</pre>';
  85. }
  86. /**
  87. *
  88. * Access API "helloWorld" using GET through Phil's rest client
  89. */
  90. public function helloWorld()
  91. {
  92. echo '<pre>';
  93. print_r($this->rest->get('helloWorld/format/json'));
  94. echo '</pre>';
  95. }
  96. /**
  97. *
  98. * Access API "contacts" using GET through Phil's rest client
  99. */
  100. public function getContacts()
  101. {
  102. echo '<pre>';
  103. print_r($this->rest->get('contacts/format/json'));
  104. echo '</pre>';
  105. }
  106. /**
  107. *
  108. * Access API "API" using GET through Phil's rest client
  109. */
  110. public function getApi()
  111. {
  112. echo '<pre>';
  113. print_r($this->rest->get('API/format/json'));
  114. echo '</pre>';
  115. }
  116. }
  117. /* End of rc.php */