http_channel.class.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace net;
  3. use Curl;
  4. /**
  5. * Description of channel
  6. *
  7. * @copyright (c) 2012 University of Geneva
  8. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  9. * @author Laurent Opprecht <laurent@opprecht.info>
  10. */
  11. class HttpChannel
  12. {
  13. /**
  14. *
  15. * @param string $url
  16. * @param type $modules
  17. * @return HttpChannel
  18. */
  19. static function create($url, $modules = array())
  20. {
  21. return new self($url, $modules);
  22. }
  23. protected $base_url = '';
  24. protected $modules = array();
  25. public function __construct($base_url = '', $modules = array())
  26. {
  27. $this->base_url = $base_url;
  28. $this->modules = $modules;
  29. }
  30. function modules()
  31. {
  32. return $this->modules;
  33. }
  34. function get($url, $parameters)
  35. {
  36. $options = $this->get_options();
  37. $url = $this->base_url . $url;
  38. return Curl::get($url, $options)->execute();
  39. }
  40. function post($url, $fields)
  41. {
  42. $options = $this->get_options();
  43. $url = $this->base_url . $url;
  44. return Curl::post($url, $fields, $options)->execute();
  45. }
  46. protected function get_options()
  47. {
  48. $result = array();
  49. $modules = $this->modules();
  50. foreach ($modules as $module) {
  51. if (is_array($module)) {
  52. $options = $module;
  53. } else {
  54. $options = $module->get_options();
  55. }
  56. $result = array_merge($result, $options);
  57. }
  58. return $result;
  59. }
  60. }