ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTTPRequest.php
Go to the documentation of this file.
1 <?php
2 
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
4 
5 // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
6 
40 // }}}
41 
42 // dependencies {{{
43 require_once 'XML/RPC2/Exception.php';
44 require_once 'XML/RPC2/Client.php';
45 // }}}
46 
59 {
60 
61  // {{{ properties
62 
68  private $_proxy = null;
69 
75  private $_proxyAuth = null;
76 
82  private $_postData;
83 
89  private $_uri;
90 
96  private $_encoding='iso-8859-1';
97 
103  private $_sslverify=true;
104 
105  // }}}
106  // {{{ getBody()
107 
113  public function getBody()
114  {
115  return $this->_body;
116  }
117 
118  // }}}
119  // {{{ setPostData()
120 
126  public function setPostData($value)
127  {
128  $this->_postData = $value;
129  }
130 
131  // }}}
132  // {{{ constructor
133 
146  public function __construct($uri = '', $params = array())
147  {
148  if (!preg_match('/(https?:\/\/)(.*)/', $uri)) throw new XML_RPC2_Exception('Unable to parse URI');
149  $this->_uri = $uri;
150  if (isset($params['encoding'])) {
151  $this->_encoding = $params['encoding'];
152  }
153  if (isset($params['proxy'])) {
154  $proxy = $params['proxy'];
155  $elements = parse_url($proxy);
156  if (is_array($elements)) {
157  if ((isset($elements['scheme'])) and (isset($elements['host']))) {
158  $this->_proxy = $elements['scheme'] . '://' . $elements['host'];
159  }
160  if (isset($elements['port'])) {
161  $this->_proxy = $this->_proxy . ':' . $elements['port'];
162  }
163  if ((isset($elements['user'])) and (isset($elements['pass']))) {
164  $this->_proxyAuth = $elements['user'] . ':' . $elements['pass'];
165  }
166  }
167  }
168  if (isset($params['sslverify'])) {
169  $this->_sslverify = $params['sslverify'];
170  }
171  }
172 
173  // }}}
174  // {{{ sendRequest()
175 
182  public function sendRequest()
183  {
184  if (!function_exists('curl_init') &&
185  !( // TODO Use PEAR::loadExtension once PEAR passes PHP5 unit tests (E_STRICT compliance, namely)
186  @dl('php_curl' . PHP_SHLIB_SUFFIX) || @dl('curl' . PHP_SHLIB_SUFFIX)
187  )) {
188  throw new XML_RPC2_CurlException('cURI extension is not present and load failed');
189  }
190  if ($ch = curl_init()) {
191  if (
192  (is_null($this->_proxy) || curl_setopt($ch, CURLOPT_PROXY, $this->_proxy)) &&
193  (is_null($this->_proxyAuth) || curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_proxyAuth)) &&
194  curl_setopt($ch, CURLOPT_URL, $this->_uri) &&
195  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) &&
196  curl_setopt($ch, CURLOPT_POST, 1) &&
197  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_sslverify) &&
198  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml; charset='.$this->_encoding, 'User-Agent: PEAR_XML_RCP2/' . XML_RPC2_Client::VERSION)) &&
199  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postData)
200  ) {
201  $result = curl_exec($ch);
202  if (($errno = curl_errno($ch)) != 0) {
203  throw new XML_RPC2_CurlException("Curl returned non-null errno $errno:" . curl_error($ch));
204  }
205  $info = curl_getinfo($ch);
206  if ($info['http_code'] != 200) {
207  throw new XML_RPC2_ReceivedInvalidStatusCodeException('Curl returned non 200 HTTP code: ' . $info['http_code'] . '. Response body:' . $result);
208  }
209  } else {
210  throw new XML_RPC2_CurlException('Unable to setup curl');
211  }
212  } else {
213  throw new XML_RPC2_CurlException('Unable to init curl');
214  }
215  $this->_body = $result;
216  return true;
217  }
218 
219  // }}}
220 
221 }
222 
223 ?>