forked from gardon/SCORMCloud_PHPLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceRequest.php
More file actions
238 lines (203 loc) · 7.95 KB
/
ServiceRequest.php
File metadata and controls
238 lines (203 loc) · 7.95 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/* Software License Agreement (BSD License)
*
* Copyright (c) 2010-2011, Rustici Software, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Rustici Software, LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once 'Configuration.php';
require_once 'DebugLogger.php';
class ServiceRequest{
/**
* Number of seconds to wait while connecting to the server.
*/
const TIMEOUT_CONNECTION = 30;
/**
* Total number of seconds to wait for a request.
*/
const TIMEOUT_TOTAL = 500;
private $_configuration = null;
private $_methodParams = array();
private $_fileToPost = null;
public function __construct($configuration) {
$this->_configuration = $configuration;
$this->_methodParams['applib'] = 'php';
//$this->_methodParams['appname'] = '';
}
public function setMethodParams($paramsArray)
{
$this->_methodParams = array_merge($this->_methodParams, $paramsArray);
}
public function setFileToPost($fileName)
{
$this->_fileToPost = $fileName;
}
public function CallService($methodName, $serviceUrl = null)
{
$postParams = null;
if(isset($this->_fileToPost))
{
//echo '_fileToPost='.$this->_fileToPost;
//TODO - check to see if this file exists
$fileContents = file_get_contents($this->_fileToPost);
$postParams = array('filedata' => "@$this->_fileToPost");
}
//$responseText = Encoding.GetEncoding("utf-8").GetString(responseBytes);
$url = $this->ConstructUrl($methodName, $serviceUrl);
//echo $url.'<br><br>';
$responseText = $this->submitHttpPost($url,$postParams);
//error_log($responseText);
$response = $this->AssertNoErrorAndReturnXml($responseText);
return $response;
}
public function ConstructUrl($methodName, $serviceUrl = null)
{
//error_log('serviceUrl = '.$serviceUrl);
$parameterMap = array( 'method' => $methodName,
'appid' => $this->_configuration->getAppId(),
'origin' => $this->_configuration->getOriginString(),
'ts' => gmdate("YmdHis")
);
array_merge($parameterMap,$this->_methodParams);
foreach($this->_methodParams as $key => $value)
{
$parameterMap[$key] = $value;
}
if(isset($serviceUrl))
{
$url = $serviceUrl.'/api';
}else{
$url = $this->_configuration->getScormEngineServiceUrl().'/api';
}
$url .= '?'.$this->signParams($this->_configuration->getSecurityKey(),$parameterMap);
write_log("SCORM Cloud ConstructUrl : ".$url);
return $url;
}
/// <summary>
/// This method will evaluate the reponse string and manually validate
/// the top-level structure. If an err is present, this will be turned
/// into a Service Exception.
/// </summary>
/// <param name="xmlString">Response from web service as xml</param>
/// <returns>XML document from the given string, provided no service errors are present</returns>
private static function AssertNoErrorAndReturnXml($xmlString)
{
$xmlDoc = simplexml_load_string($xmlString);
$rspElements = $xmlDoc;
write_log('stat : '.$rspElements["stat"]);
if($rspElements["stat"]!='ok')
{
$errmsg = "";
if($rspElements["stat"]=='fail')
{
$errmsg = "SCORM Cloud Error : ".$rspElements->err["code"]." - ".$rspElements->err["msg"];
}else{
$errmsg = "Invalid XML Response from web service call, expected <rsp> tag, instead received: ".$xmlString;
}
error_log($errmsg);
throw new Exception($errmsg);
}
return $xmlString;
}
/**
* Submit a POST request with to the specified URL with given parameters.
*
* @param string $url
* @param array $params An optional array of parameter name/value
* pairs to include in the POST.
* @param integer $timeout The total number of seconds, including the
* wait for the initial connection, wait for a request to complete.
* @return string
* @throws Phlickr_ConnectionException
* @uses TIMEOUT_CONNECTION to determine how long to wait for a
* for a connection.
* @uses TIMEOUT_TOTAL to determine how long to wait for a request
* to complete.
* @uses set_time_limit() to ensure that PHP's script timer is five
* seconds longer than the sum of $timeout and TIMEOUT_CONNECTION.
*/
static function submitHttpPost($url, $postParams = null, $timeout = self::TIMEOUT_TOTAL)
{
//foreach($postParams as $key => $value)
//{
// echo $key.'='.$value.'<br>';
//}
$ch = curl_init();
// set up the request
curl_setopt($ch, CURLOPT_URL, $url);
// make sure we submit this as a post
curl_setopt($ch, CURLOPT_POST, true);
if (isset($postParams)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
}
// make sure problems are caught
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
// return the output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set the timeouts
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT_CONNECTION);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
//set header expect empty for upload issue...
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// set the PHP script's timeout to be greater than CURL's
set_time_limit(self::TIMEOUT_CONNECTION + $timeout + 5);
$result = curl_exec($ch);
// check for errors
if (0 == curl_errno($ch)) {
curl_close($ch);
return $result;
} else {
//$ex = new Phlickr_ConnectionException(
//echo 'Request failed. '.curl_error($ch);
curl_close($ch);
//throw $ex;
}
}
/**
* Create a signed signature of the parameters.
*
* Return a parameter string that can be tacked onto the end of a URL.
* Items will be sorted and an api_sig element will be on the end.
*/
static function signParams($secret, $params)
{
$signing = '';
$values = array();
if (defined('SORT_FLAG_CASE')) { //PHP 5.4 and higher only.
ksort($params, SORT_STRING | SORT_FLAG_CASE);
} else {
ksort($params, SORT_STRING);
}
foreach($params as $key => $value) {
$signing .= $key . $value;
$values[] = $key . '=' . urlencode($value);
}
$values[] = 'sig=' . md5($secret . $signing);
return implode('&', $values);
}
}
?>