This repository was archived by the owner on Jun 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathHTTPMessage.php
More file actions
155 lines (126 loc) · 4.82 KB
/
HTTPMessage.php
File metadata and controls
155 lines (126 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace IMSGlobal\LTI;
/**
* Class to represent an HTTP message.
*
* @author Stephen P Vickers <svickers@imsglobal.org>
* @copyright 2016 IMS Global Learning Consortium Inc
* @version 3.0.0
* @license Apache-2.0
*/
class HTTPMessage
{
/** @var bool True if message was sent successfully. */
public $ok = false;
/** @var request Request body. */
public $request = null;
/** @var request_headers Request headers. */
public $requestHeaders = '';
/** @var response Response body. */
public $response = null;
/** @var response_headers Response headers. */
public $responseHeaders = '';
/** @var status Status of response (0 if undetermined). */
public $status = 0;
/** @var error Error message. */
public $error = '';
/** @var url Request URL. */
private $url = null;
/** @var method Request method. */
private $method = null;
/**
* Class constructor.
*
* @param string $url URL to send request to.
* @param string $method Request method to use (optional, default is GET).
* @param mixed $params Associative array of parameter values to be passed or message body (optional, default is none).
* @param string $header Values to include in the request header (optional, default is none).
*/
public function __construct($url, $method = 'GET', $params = null, $header = null)
{
$this->url = $url;
$this->method = strtoupper($method);
if (is_array($params)) {
$this->request = http_build_query($params);
} else {
$this->request = $params;
}
if (!empty($header)) {
$this->requestHeaders = explode("\n", $header);
}
}
/**
* Send the request to the target URL.
*
* @return bool TRUE if the request was successful
*/
public function send()
{
$this->ok = false;
// Try using curl if available
if (function_exists('curl_init')) {
$resp = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
if (!empty($this->requestHeaders)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if ($this->method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
} elseif ($this->method !== 'GET') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
if (!is_null($this->request)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
}
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_SSLVERSION,3);
$chResp = curl_exec($ch);
$this->ok = $chResp !== false;
if ($this->ok) {
$chResp = str_replace("\r\n", "\n", $chResp);
$chRespSplit = explode("\n\n", $chResp, 2);
if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) {
$chRespSplit = explode("\n\n", $chRespSplit[1], 2);
}
$this->responseHeaders = $chRespSplit[0];
$resp = $chRespSplit[1];
$this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->ok = $this->status < 400;
if (!$this->ok) {
$this->error = curl_error($ch);
}
}
$this->requestHeaders = str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
curl_close($ch);
$this->response = $resp;
} else {
// Try using fopen if curl was not available
$opts = array(
'method' => $this->method,
'content' => $this->request
);
if (!empty($this->requestHeaders)) {
$opts['header'] = $this->requestHeaders;
}
try {
$ctx = stream_context_create(array(
'http' => $opts
));
$fp = @fopen($this->url, 'rb', false, $ctx);
if ($fp) {
$resp = @stream_get_contents($fp);
$this->ok = $resp !== false;
}
} catch (\Exception $e) {
$this->ok = false;
}
}
return $this->ok;
}
}