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
180 lines (161 loc) · 4.76 KB
/
Copy pathHTTPMessage.php
File metadata and controls
180 lines (161 loc) · 4.76 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace IMSGlobal\LTI;
/**
* Class to represent an HTTP message
*
* @author Stephen P Vickers <svickers@imsglobal.org>
* @copyright IMS Global Learning Consortium Inc
* @date 2016
* @version 3.0.0
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
class HTTPMessage
{
/**
* True if message was sent successfully.
*
* @var boolean $ok
*/
public $ok = false;
/**
* Request body.
*
* @var request $request
*/
public $request = null;
/**
* Request headers.
*
* @var request_headers $requestHeaders
*/
public $requestHeaders = '';
/**
* Response body.
*
* @var response $response
*/
public $response = null;
/**
* Response headers.
*
* @var response_headers $responseHeaders
*/
public $responseHeaders = '';
/**
* Status of response (0 if undetermined).
*
* @var status $status
*/
public $status = 0;
/**
* Error message
*
* @var error $error
*/
public $error = '';
/**
* Request URL.
*
* @var url $url
*/
private $url = null;
/**
* Request method.
*
* @var method $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)
*/
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 boolean 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);
} else if ($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;
$this->response = $resp;
}
} catch (\Exception $e) {
$this->ok = false;
}
}
return $this->ok;
}
}