| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2010 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* HTTP Request used to execute http requests using the apiIO classes. On execution the |
| 20 |
* responseHttpCode, responseHeaders and responseBody will be filled in. |
| 21 |
* |
| 22 |
* @author Chris Chabot <chabotc@google.com> |
| 23 |
* @author Chirag Shah <chirags@google.com> |
| 24 |
* |
| 25 |
*/ |
| 26 |
class apiHttpRequest { |
| 27 |
const USER_AGENT_SUFFIX = "google-api-php-client/0.4.7"; |
| 28 |
|
| 29 |
protected $url; |
| 30 |
protected $method; |
| 31 |
protected $headers; |
| 32 |
protected $postBody; |
| 33 |
protected $userAgent; |
| 34 |
|
| 35 |
protected $responseHttpCode; |
| 36 |
protected $responseHeaders; |
| 37 |
protected $responseBody; |
| 38 |
|
| 39 |
public $accessKey; |
| 40 |
|
| 41 |
public function __construct($url, $method = 'GET', $headers = array(), $postBody = null) { |
| 42 |
$this->url = $url; |
| 43 |
// force the method name to always be upper case so we can do sane comparisons on it |
| 44 |
$this->method = strtoupper($method); |
| 45 |
$this->headers = $headers; |
| 46 |
$this->postBody = $postBody; |
| 47 |
|
| 48 |
global $apiConfig; |
| 49 |
if (empty($apiConfig['application_name'])) { |
| 50 |
$this->userAgent = apiHttpRequest::USER_AGENT_SUFFIX; |
| 51 |
} else { |
| 52 |
$this->userAgent = $apiConfig['application_name'] . " " . apiHttpRequest::USER_AGENT_SUFFIX; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Misc function that returns the base url component of the $url |
| 58 |
* used by the OAuth signing class to calculate the base string |
| 59 |
* @return string The base url component of the $url. |
| 60 |
* @see http://oauth.net/core/1.0a/#anchor13 |
| 61 |
*/ |
| 62 |
public function getBaseUrl() { |
| 63 |
if ($pos = strpos($this->url, '?')) { |
| 64 |
return substr($this->url, 0, $pos); |
| 65 |
} |
| 66 |
return $this->url; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Misc function that returns an array of the query parameters of the current url |
| 71 |
* used by the OAuth signing class to calculate the signature |
| 72 |
* @return array Query parameters in the query string. |
| 73 |
*/ |
| 74 |
public function getQueryParams() { |
| 75 |
if ($pos = strpos($this->url, '?')) { |
| 76 |
$queryStr = substr($this->url, $pos + 1); |
| 77 |
$params = array(); |
| 78 |
parse_str($queryStr, $params); |
| 79 |
return $params; |
| 80 |
} |
| 81 |
return array(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return string HTTP Response Code. |
| 86 |
*/ |
| 87 |
public function getResponseHttpCode() { |
| 88 |
return $this->responseHttpCode; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param int $responseHttpCode HTTP Response Code. |
| 93 |
*/ |
| 94 |
public function setResponseHttpCode($responseHttpCode) { |
| 95 |
$this->responseHttpCode = $responseHttpCode; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return $responseHeaders (array) HTTP Response Headers. |
| 100 |
*/ |
| 101 |
public function getResponseHeaders() { |
| 102 |
return $this->responseHeaders; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return string HTTP Response Body |
| 107 |
*/ |
| 108 |
public function getResponseBody() { |
| 109 |
return $this->responseBody; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param array $responseHeaders The HTTP Response Headers. |
| 114 |
*/ |
| 115 |
public function setResponseHeaders($responseHeaders) { |
| 116 |
$this->responseHeaders = $responseHeaders; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param string $responseBody $responseBody to set. |
| 121 |
*/ |
| 122 |
public function setResponseBody($responseBody) { |
| 123 |
$this->responseBody = $responseBody; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @return string $url The request URL. |
| 128 |
*/ |
| 129 |
|
| 130 |
public function getUrl() { |
| 131 |
return $this->url; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @return string $method |
| 136 |
*/ |
| 137 |
public function getMethod() { |
| 138 |
return $this->method; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @return array the $headers |
| 143 |
*/ |
| 144 |
public function getHeaders() { |
| 145 |
return $this->headers; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @return string the $postBody |
| 150 |
*/ |
| 151 |
public function getPostBody() { |
| 152 |
return $this->postBody; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param string $url the url to set |
| 157 |
*/ |
| 158 |
public function setUrl($url) { |
| 159 |
$this->url = $url; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @param string $method the method to set |
| 164 |
*/ |
| 165 |
public function setMethod($method) { |
| 166 |
$this->method = $method; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @param array $headers the headers to set |
| 171 |
*/ |
| 172 |
public function setHeaders($headers) { |
| 173 |
$this->headers = $headers; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @param string $header the header to add. |
| 178 |
*/ |
| 179 |
public function addHeader($header) { |
| 180 |
if (null == $this->headers) { |
| 181 |
$this->headers = array(); |
| 182 |
} |
| 183 |
|
| 184 |
$this->headers[] = $header; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* @param string $postBody the postBody to set |
| 189 |
*/ |
| 190 |
public function setPostBody($postBody) { |
| 191 |
$this->postBody = $postBody; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Set the User-Agent Header. |
| 196 |
* @param string $userAgent The User-Agent. |
| 197 |
*/ |
| 198 |
public function setUserAgent($userAgent) { |
| 199 |
$this->userAgent = $userAgent; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* @return string The User-Agent. |
| 204 |
*/ |
| 205 |
public function getUserAgent() { |
| 206 |
return $this->userAgent; |
| 207 |
} |
| 208 |
} |
| 209 |
|