LTI Tool Provider Library  3.0.2
PHP class library for building LTI Tool Providers
HTTPMessage.php
Go to the documentation of this file.
1 <?php
2 
3 namespace IMSGlobal\LTI;
4 
15 {
16 
22  public $ok = false;
23 
29  public $request = null;
30 
36  public $requestHeaders = '';
37 
43  public $response = null;
44 
50  public $responseHeaders = '';
51 
57  public $status = 0;
58 
64  public $error = '';
65 
71  private $url = null;
72 
78  private $method = null;
79 
88  function __construct($url, $method = 'GET', $params = null, $header = null)
89  {
90 
91  $this->url = $url;
92  $this->method = strtoupper($method);
93  if (is_array($params)) {
94  $this->request = http_build_query($params);
95  } else {
96  $this->request = $params;
97  }
98  if (!empty($header)) {
99  $this->requestHeaders = explode("\n", $header);
100  }
101 
102  }
103 
109  public function send()
110  {
111 
112  $this->ok = false;
113 // Try using curl if available
114  if (function_exists('curl_init')) {
115  $resp = '';
116  $ch = curl_init();
117  curl_setopt($ch, CURLOPT_URL, $this->url);
118  if (!empty($this->requestHeaders)) {
119  curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
120  } else {
121  curl_setopt($ch, CURLOPT_HEADER, 0);
122  }
123  if ($this->method === 'POST') {
124  curl_setopt($ch, CURLOPT_POST, true);
125  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
126  } else if ($this->method !== 'GET') {
127  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
128  if (!is_null($this->request)) {
129  curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
130  }
131  }
132  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
133  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
134  curl_setopt($ch, CURLOPT_HEADER, true);
135  curl_setopt($ch, CURLOPT_SSLVERSION,3);
136  $chResp = curl_exec($ch);
137  $this->ok = $chResp !== false;
138  if ($this->ok) {
139  $chResp = str_replace("\r\n", "\n", $chResp);
140  $chRespSplit = explode("\n\n", $chResp, 2);
141  if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
142  $chRespSplit = explode("\n\n", $chRespSplit[1], 2);
143  }
144  $this->responseHeaders = $chRespSplit[0];
145  $resp = $chRespSplit[1];
146  $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
147  $this->ok = $this->status < 400;
148  if (!$this->ok) {
149  $this->error = curl_error($ch);
150  }
151  }
152  $this->requestHeaders = str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
153  curl_close($ch);
154  $this->response = $resp;
155  } else {
156 // Try using fopen if curl was not available
157  $opts = array('method' => $this->method,
158  'content' => $this->request
159  );
160  if (!empty($this->requestHeaders)) {
161  $opts['header'] = $this->requestHeaders;
162  }
163  try {
164  $ctx = stream_context_create(array('http' => $opts));
165  $fp = @fopen($this->url, 'rb', false, $ctx);
166  if ($fp) {
167  $resp = @stream_get_contents($fp);
168  $this->ok = $resp !== false;
169  }
170  } catch (\Exception $e) {
171  $this->ok = false;
172  }
173  }
174 
175  return $this->ok;
176 
177  }
178 
179 }
send()
Send the request to the target URL.
__construct($url, $method= 'GET', $params=null, $header=null)
Class constructor.
Definition: HTTPMessage.php:88
$response
Response body.
Definition: HTTPMessage.php:43
$status
Status of response (0 if undetermined).
Definition: HTTPMessage.php:57
$requestHeaders
Request headers.
Definition: HTTPMessage.php:36
$responseHeaders
Response headers.
Definition: HTTPMessage.php:50
$ok
True if message was sent successfully.
Definition: HTTPMessage.php:22
Class to represent an HTTP message.
Definition: HTTPMessage.php:14