apiHttpRequest.php
4.8 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
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* HTTP Request used to execute http requests using the apiIO classes. On execution the
* responseHttpCode, responseHeaders and responseBody will be filled in.
*
* @author Chris Chabot <chabotc@google.com>
*
*/
class apiHttpRequest {
const USER_AGENT_SUFFIX = "google-api-php-client/0.4.4";
public $url;
public $method;
public $headers;
public $postBody;
public $userAgent;
public $responseHttpCode;
public $responseHeaders;
public $responseBody;
public $accessKey;
public function __construct($url, $method = 'GET', $headers = array(), $postBody = null) {
$this->url = $url;
// force the method name to always be upper case so we can do sane comparisons on it
$this->method = strtoupper($method);
$this->headers = $headers;
$this->postBody = $postBody;
global $apiConfig;
if (empty($apiConfig['application_name'])) {
$this->userAgent = apiHttpRequest::USER_AGENT_SUFFIX;
} else {
$this->userAgent = $apiConfig['application_name'] . " " . apiHttpRequest::USER_AGENT_SUFFIX;
}
}
/**
* Misc function that returns the base url component of the $url
* used by the OAuth signing class to calculate the base string
* @return string The base url component of the $url.
* @see http://oauth.net/core/1.0a/#anchor13
*/
public function getBaseUrl() {
if ($pos = strpos($this->url, '?')) {
return substr($this->url, 0, $pos);
}
return $this->url;
}
/**
* Misc function that returns an array of the query parameters of the current url
* used by the OAuth signing class to calculate the signature
* @return array Query parameters in the query string.
*/
public function getQueryParams() {
if ($pos = strpos($this->url, '?')) {
$queryStr = substr($this->url, $pos + 1);
$params = array();
parse_str($queryStr, $params);
return $params;
}
return array();
}
/**
* @return string the HTTP Response Code.
*/
public function getResponseHttpCode() {
return $this->responseHttpCode;
}
/**
* @param $responseHttpCode the $responseHttpCode to set.
*/
public function setResponseHttpCode($responseHttpCode) {
$this->responseHttpCode = $responseHttpCode;
}
/**
* @return array the $responseHeaders
*/
public function getResponseHeaders() {
return $this->responseHeaders;
}
/**
* @return string the $responseBody
*/
function getResponseBody() {
return $this->responseBody;
}
/**
* @param array $responseHeaders the $responseHeaders to set.
*/
public function setResponseHeaders($responseHeaders) {
$this->responseHeaders = $responseHeaders;
}
/**
* @param string $responseBody the $responseBody to set.
*/
public function setResponseBody($responseBody) {
$this->responseBody = $responseBody;
}
/**
* @return string $url The request URL.
*/
public function getUrl() {
return $this->url;
}
/**
* @return string the $method
*/
public function getMethod() {
return $this->method;
}
/**
* @return array the $headers
*/
public function getHeaders() {
return $this->headers;
}
/**
* @return string the $postBody
*/
public function getPostBody() {
return $this->postBody;
}
/**
* @param string $url the url to set
*/
public function setUrl($url) {
$this->url = $url;
}
/**
* @param string $method the method to set
*/
public function setMethod($method) {
$this->method = $method;
}
/**
* @param array $headers the headers to set
*/
public function setHeaders($headers) {
$this->headers = $headers;
}
/**
* @param string $header the header to add.
*/
public function addHeader($header) {
if (null == $this->headers) {
$this->headers = array();
}
$this->headers[] = $header;
}
/**
* @param string $postBody the postBody to set
*/
public function setPostBody($postBody) {
$this->postBody = $postBody;
}
/**
* Set the User-Agent Header.
* @param string $userAgent The User-Agent.
*/
public function setUserAgent($userAgent) {
$this->userAgent = $userAgent;
}
/**
* @return string The User-Agent.
*/
public function getUserAgent() {
return $this->userAgent;
}
}