1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/Modules/HTTP |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv3\Modules\HTTP; |
45
|
|
|
|
46
|
|
|
use DataSift\Stone\HttpLib\HttpClient; |
47
|
|
|
use DataSift\Stone\HttpLib\HttpClientRequest; |
48
|
|
|
use DataSift\Stone\HttpLib\HttpClientResponse; |
49
|
|
|
|
50
|
|
|
use Prose\Prose; |
51
|
|
|
|
52
|
|
|
use Storyplayer\SPv3\Modules\Exceptions; |
53
|
|
|
use Storyplayer\SPv3\Modules\Log; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* do things to a web site by making requests directly to it (i.e. not |
57
|
|
|
* using the web browser at all) |
58
|
|
|
* |
59
|
|
|
* Great for testing APIs |
60
|
|
|
* |
61
|
|
|
* @category Libraries |
62
|
|
|
* @package Storyplayer/Modules/HTTP |
63
|
|
|
* @author Stuart Herbert <[email protected]> |
64
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
65
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
66
|
|
|
* @link http://datasift.github.io/storyplayer |
67
|
|
|
*/ |
68
|
|
|
class UsingHttp extends Prose |
69
|
|
|
{ |
70
|
|
|
public function delete($url, $params = array(), $headers = array(), $timeout = null) |
71
|
|
|
{ |
72
|
|
|
return $this->makeHttpRequest($url, "DELETE", $params, null, $headers, $timeout); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function post($url, $params = array(), $body = null, $headers = array(), $timeout = null) |
76
|
|
|
{ |
77
|
|
|
return $this->makeHttpRequest($url, "POST", $params, $body, $headers, $timeout); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function put($url, $params = array(), $body = null, $headers = array(), $timeout = null) |
81
|
|
|
{ |
82
|
|
|
return $this->makeHttpRequest($url, "PUT", $params, $body, $headers, $timeout); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $verb |
87
|
|
|
*/ |
88
|
|
|
protected function makeHttpRequest($url, $verb, $params, $body, $headers = array(), $timeout = null) |
89
|
|
|
{ |
90
|
|
|
// create the full URL |
91
|
|
|
if (count($params) > 0) { |
92
|
|
|
$url = $url . '?' . http_build_query($params); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// what are we doing? |
96
|
|
|
$logMsg = [ "HTTP " . strtoupper($verb) . " '${url}'" ]; |
97
|
|
|
if ($body != null) { |
98
|
|
|
$logMsg[] = $body; |
99
|
|
|
} |
100
|
|
|
if (count($headers) > 0) { |
101
|
|
|
$logMsg[] = $headers; |
102
|
|
|
} |
103
|
|
|
$log = Log::usingLog()->startAction($logMsg); |
104
|
|
|
|
105
|
|
|
// build the HTTP request |
106
|
|
|
$request = new HttpClientRequest($url); |
107
|
|
|
$request->withUserAgent("Storyplayer") |
108
|
|
|
->withHttpVerb($verb); |
109
|
|
|
|
110
|
|
|
if (is_array($headers)) { |
111
|
|
|
foreach ($headers as $key => $value) { |
112
|
|
|
$request->withExtraHeader($key, $value); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (is_array($body)) { |
117
|
|
|
foreach ($body as $key => $value) { |
118
|
|
|
$request->addData($key, $value); |
119
|
|
|
} |
120
|
|
|
}else{ |
121
|
|
|
$request->setPayload($body); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// special case - do we validate SSL certificates in this |
125
|
|
|
// test environment? |
126
|
|
|
$validateSsl = fromConfig()->getModuleSetting("http.validateSsl"); |
127
|
|
|
if (null === $validateSsl) { |
128
|
|
|
// default to TRUE if no setting present |
129
|
|
|
$validateSsl = true; |
130
|
|
|
} |
131
|
|
|
if (!$validateSsl) { |
132
|
|
|
$request->disableSslCertificateValidation(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($timeout !== null) { |
136
|
|
|
$request->setReadTimeout($timeout); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// make the call |
140
|
|
|
$client = new HttpClient(); |
141
|
|
|
$response = $client->newRequest($request); |
142
|
|
|
|
143
|
|
|
// is this a valid response? |
144
|
|
|
if (!$response instanceof HttpClientResponse) { |
145
|
|
|
throw Exceptions::newActionFailedException(__METHOD__); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// all done |
149
|
|
|
$log->endAction($response); |
150
|
|
|
return $response; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|