-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpAction.class.php
118 lines (88 loc) · 2.85 KB
/
HttpAction.class.php
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
<?php
require_once("Action.class.php");
abstract class HttpAction extends Action {
private $curl;
private $response;
private $withResponse = true;
private $withFollowLocation = true;
private $unformattedUrl = ""; /* URL ENCODED! */
/* '%variablename' => 'value' - URL ENCODED! */
private $variables = [];
private $httpHeaders;
public function __construct($id, $name, $unformattedUrl, $httpHeaders = []) {
parent::__construct($id, $name);
$this->setUnformattedUrl($unformattedUrl);
$this->setHttpHeaders($httpHeaders);
}
/* not final: Overridden @HttpPostAction, @OpenHabRESTAction, @HttpGetStateQueryAction */
protected function before() {
curl_setopt($this->getCurl(), CURLOPT_RETURNTRANSFER, $this->withResponse);
curl_setopt($this->getCurl(), CURLOPT_FOLLOWLOCATION, $this->withFollowLocation);
curl_setopt($this->getCurl(), CURLOPT_HTTPHEADER, $this->getHttpHeaders());
}
/* not final: Overridden @HttpGetStateQueryAction */
protected function main(){
curl_setopt($this->curl, CURLOPT_URL, $this->getFormattedUrl());
$this->response = curl_exec($this->curl);
echo $this->response;
}
final protected function after(){
curl_close($this->getCurl());
}
public function getFormattedUrl(){
$formattedVars = array();
foreach ($this->variables as $key => $value) {
$formattedVars[$key] = urlencode($value);
}
return str_replace(array_keys($formattedVars), array_values($formattedVars), $this->unformattedUrl);
}
public function setVariable($key, $stringValue) { //unencoded String values!
$this->addVariable($key, $stringValue);
}
public function addVariable($key, $stringValue) { //unencoded String values!
$this->variables[$key] = $stringValue;
}
public function setVariables($variables) {
$this->variables = $variables;
}
public function getVariables() {
return $this->variables;
}
public function setHttpHeader($header) {
$this->addHttpHeader($header);
}
public function addHttpHeader($header) { //unencoded String values!
$this->httpHeaders[] = $header;
}
public function setHttpHeaders($httpHeaders) {
$this->httpHeaders = $httpHeaders;
}
public function getHttpHeaders() {
return $this->httpHeaders;
}
public function setUnformattedUrl($unformattedUrl){
$this->unformattedUrl = $unformattedUrl;
}
public function getUnformattedUrl(){
return $this->unformattedUrl;
}
public function getResponse() {
return $this->response;
}
public function setWithResponse($withResponse = true) {
$this->withResponse = $withResponse;
}
public function setWithFollowLocation($withFollowLocation = true) {
$this->withFollowLocation = $withFollowLocation;
}
public function getCurl(){
if ($this->curl === null) {
$this->curl = curl_init();
}
return $this->curl;
}
public function setCurlOption($optionname, $optionvalue) {
curl_setopt($this->getCurl(), $optionname, $optionvalue);
}
}
?>