curl.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. *
  4. * @copyright (c) 2012 University of Geneva
  5. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  6. * @author Laurent Opprecht <laurent@opprecht.info>
  7. */
  8. class Curl
  9. {
  10. protected static $default_options = array();
  11. static function get_default_options($options = array())
  12. {
  13. if (empty(self::$default_options)) {
  14. self::$default_options[CURLOPT_HEADER] = false;
  15. self::$default_options[CURLOPT_RETURNTRANSFER] = true;
  16. self::$default_options[CURLOPT_SSL_VERIFYPEER] = false;
  17. }
  18. $result = self::$default_options;
  19. foreach ($options as $key => $value) {
  20. $result[$key] = $value;
  21. }
  22. return $result;
  23. }
  24. static function set_default_option($key, $value)
  25. {
  26. $options = $this->get_options(array($key => $value));
  27. self::$default_options = $options;
  28. }
  29. /**
  30. *
  31. * @param string $url
  32. * @param array $options
  33. * @return Curl
  34. */
  35. static function get($url, $options = array())
  36. {
  37. $options[CURLOPT_HTTPGET] = true;
  38. $result = new self($url, $options);
  39. return $result;
  40. }
  41. /**
  42. *
  43. * @param string $url
  44. * @param array $fields
  45. * @param array $options
  46. * @return Curl
  47. */
  48. static function post($url, $fields, $options = array())
  49. {
  50. $options[CURLOPT_POST] = true;
  51. $options[CURLOPT_POSTFIELDS] = $fields;
  52. $result = new self($url, $options);
  53. return $result;
  54. }
  55. protected $url = '';
  56. protected $options = array();
  57. protected $content = '';
  58. protected $info = array();
  59. protected $error = '';
  60. protected $error_no = 0;
  61. function __construct($url, $options = array())
  62. {
  63. $this->url = $url;
  64. $this->options = self::get_default_options($options);
  65. }
  66. function url()
  67. {
  68. return $this->url;
  69. }
  70. function options()
  71. {
  72. return $this->options;
  73. }
  74. function execute()
  75. {
  76. $ch = curl_init();
  77. $options = $this->options;
  78. $options[CURLOPT_URL] = $this->url;
  79. curl_setopt_array($ch, $options);
  80. $this->content = curl_exec($ch);
  81. $this->error = curl_error($ch);
  82. $this->info = curl_getinfo($ch);
  83. $this->error_no = curl_errno($ch);
  84. curl_close($ch);
  85. return $this->content;
  86. }
  87. function content()
  88. {
  89. return $this->content;
  90. }
  91. /**
  92. * @return array|string
  93. */
  94. function info($key = false)
  95. {
  96. if ($key) {
  97. return isset($this->info[$key]) ? $this->info[$key] : false;
  98. } else {
  99. return $this->info;
  100. }
  101. }
  102. function error()
  103. {
  104. return $this->error;
  105. }
  106. function error_no()
  107. {
  108. return $this->error_no;
  109. }
  110. }