Google_HttpRequest

HTTP Request to be executed by apiIO classes.

Defined (1)

The class is defined in the following location(s).

/gdwpm-api/io/Google_HttpRequest.php  
  1. class Google_HttpRequest { 
  2. const USER_AGENT_SUFFIX = "google-api-php-client/0.6.5"; 
  3. private $batchHeaders = array( 
  4. 'Content-Type' => 'application/http',  
  5. 'Content-Transfer-Encoding' => 'binary',  
  6. 'MIME-Version' => '1.0',  
  7. 'Content-Length' => '' 
  8. ); 
  9.  
  10. protected $url; 
  11. protected $requestMethod; 
  12. protected $requestHeaders; 
  13. protected $postBody; 
  14. protected $userAgent; 
  15.  
  16. protected $responseHttpCode; 
  17. protected $responseHeaders; 
  18. protected $responseBody; 
  19.  
  20. public $accessKey; 
  21.  
  22. public function __construct($url, $method = 'GET', $headers = array(), $postBody = null) { 
  23. $this->setUrl($url); 
  24. $this->setRequestMethod($method); 
  25. $this->setRequestHeaders($headers); 
  26. $this->setPostBody($postBody); 
  27.  
  28. global $gdwpm_apiConfig; 
  29. if (empty($gdwpm_apiConfig['application_name'])) { 
  30. $this->userAgent = self::USER_AGENT_SUFFIX; 
  31. } else { 
  32. $this->userAgent = $gdwpm_apiConfig['application_name'] . " " . self::USER_AGENT_SUFFIX; 
  33.  
  34. /** 
  35. * Misc function that returns the base url component of the $url 
  36. * used by the OAuth signing class to calculate the base string 
  37. * @return string The base url component of the $url. 
  38. * @see http://oauth.net/core/1.0a/#anchor13 
  39. */ 
  40. public function getBaseUrl() { 
  41. if ($pos = strpos($this->url, '?')) { 
  42. return substr($this->url, 0, $pos); 
  43. return $this->url; 
  44.  
  45. /** 
  46. * Misc function that returns an array of the query parameters of the current 
  47. * url used by the OAuth signing class to calculate the signature 
  48. * @return array Query parameters in the query string. 
  49. */ 
  50. public function getQueryParams() { 
  51. if ($pos = strpos($this->url, '?')) { 
  52. $queryStr = substr($this->url, $pos + 1); 
  53. $params = array(); 
  54. parse_str($queryStr, $params); 
  55. return $params; 
  56. return array(); 
  57.  
  58. /** 
  59. * @return string HTTP Response Code. 
  60. */ 
  61. public function getResponseHttpCode() { 
  62. return (int) $this->responseHttpCode; 
  63.  
  64. /** 
  65. * @param int $responseHttpCode HTTP Response Code. 
  66. */ 
  67. public function setResponseHttpCode($responseHttpCode) { 
  68. $this->responseHttpCode = $responseHttpCode; 
  69.  
  70. /** 
  71. * @return $responseHeaders (array) HTTP Response Headers. 
  72. */ 
  73. public function getResponseHeaders() { 
  74. return $this->responseHeaders; 
  75.  
  76. /** 
  77. * @return string HTTP Response Body 
  78. */ 
  79. public function getResponseBody() { 
  80. return $this->responseBody; 
  81.  
  82. /** 
  83. * @param array $headers The HTTP response headers 
  84. * to be normalized. 
  85. */ 
  86. public function setResponseHeaders($headers) { 
  87. $headers = Google_Utils::normalize($headers); 
  88. if ($this->responseHeaders) { 
  89. $headers = array_merge($this->responseHeaders, $headers); 
  90.  
  91. $this->responseHeaders = $headers; 
  92.  
  93. /** 
  94. * @param string $key 
  95. * @return array|boolean Returns the requested HTTP header or 
  96. * false if unavailable. 
  97. */ 
  98. public function getResponseHeader($key) { 
  99. return isset($this->responseHeaders[$key]) 
  100. ? $this->responseHeaders[$key] 
  101. : false; 
  102.  
  103. /** 
  104. * @param string $responseBody The HTTP response body. 
  105. */ 
  106. public function setResponseBody($responseBody) { 
  107. $this->responseBody = $responseBody; 
  108.  
  109. /** 
  110. * @return string $url The request URL. 
  111. */ 
  112.  
  113. public function getUrl() { 
  114. return $this->url; 
  115.  
  116. /** 
  117. * @return string $method HTTP Request Method. 
  118. */ 
  119. public function getRequestMethod() { 
  120. return $this->requestMethod; 
  121.  
  122. /** 
  123. * @return array $headers HTTP Request Headers. 
  124. */ 
  125. public function getRequestHeaders() { 
  126. return $this->requestHeaders; 
  127.  
  128. /** 
  129. * @param string $key 
  130. * @return array|boolean Returns the requested HTTP header or 
  131. * false if unavailable. 
  132. */ 
  133. public function getRequestHeader($key) { 
  134. return isset($this->requestHeaders[$key]) 
  135. ? $this->requestHeaders[$key] 
  136. : false; 
  137.  
  138. /** 
  139. * @return string $postBody HTTP Request Body. 
  140. */ 
  141. public function getPostBody() { 
  142. return $this->postBody; 
  143.  
  144. /** 
  145. * @param string $url the url to set 
  146. */ 
  147. public function setUrl($url) { 
  148. if (substr($url, 0, 4) == 'http') { 
  149. $this->url = $url; 
  150. } else { 
  151. // Force the path become relative. 
  152. if (substr($url, 0, 1) !== '/') { 
  153. $url = '/' . $url; 
  154. global $gdwpm_apiConfig; 
  155. $this->url = $gdwpm_apiConfig['basePath'] . $url; 
  156.  
  157. /** 
  158. * @param string $method Set he HTTP Method and normalize 
  159. * it to upper-case, as required by HTTP. 
  160. */ 
  161. public function setRequestMethod($method) { 
  162. $this->requestMethod = strtoupper($method); 
  163.  
  164. /** 
  165. * @param array $headers The HTTP request headers 
  166. * to be set and normalized. 
  167. */ 
  168. public function setRequestHeaders($headers) { 
  169. $headers = Google_Utils::normalize($headers); 
  170. if ($this->requestHeaders) { 
  171. $headers = array_merge($this->requestHeaders, $headers); 
  172. $this->requestHeaders = $headers; 
  173.  
  174. /** 
  175. * @param string $postBody the postBody to set 
  176. */ 
  177. public function setPostBody($postBody) { 
  178. $this->postBody = $postBody; 
  179.  
  180. /** 
  181. * Set the User-Agent Header. 
  182. * @param string $userAgent The User-Agent. 
  183. */ 
  184. public function setUserAgent($userAgent) { 
  185. $this->userAgent = $userAgent; 
  186.  
  187. /** 
  188. * @return string The User-Agent. 
  189. */ 
  190. public function getUserAgent() { 
  191. return $this->userAgent; 
  192.  
  193. /** 
  194. * Returns a cache key depending on if this was an OAuth signed request 
  195. * in which case it will use the non-signed url and access key to make this 
  196. * cache key unique per authenticated user, else use the plain request url 
  197. * @return string The md5 hash of the request cache key. 
  198. */ 
  199. public function getCacheKey() { 
  200. $key = $this->getUrl(); 
  201.  
  202. if (isset($this->accessKey)) { 
  203. $key .= $this->accessKey; 
  204.  
  205. if (isset($this->requestHeaders['authorization'])) { 
  206. $key .= $this->requestHeaders['authorization']; 
  207.  
  208. return md5($key); 
  209.  
  210. public function getParsedCacheControl() { 
  211. $parsed = array(); 
  212. $rawCacheControl = $this->getResponseHeader('cache-control'); 
  213. if ($rawCacheControl) { 
  214. $rawCacheControl = str_replace(', ', '&', $rawCacheControl); 
  215. parse_str($rawCacheControl, $parsed); 
  216.  
  217. return $parsed; 
  218.  
  219. /** 
  220. * @param string $id 
  221. * @return string A string representation of the HTTP Request. 
  222. */ 
  223. public function toBatchString($id) { 
  224. $str = ''; 
  225. foreach($this->batchHeaders as $key => $val) { 
  226. $str .= $key . ': ' . $val . "\n"; 
  227.  
  228. $str .= "Content-ID: $id\n"; 
  229. $str .= "\n"; 
  230.  
  231. $path = parse_url($this->getUrl(), PHP_URL_PATH); 
  232. $str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n"; 
  233. foreach($this->getRequestHeaders() as $key => $val) { 
  234. $str .= $key . ': ' . $val . "\n"; 
  235.  
  236. if ($this->getPostBody()) { 
  237. $str .= "\n"; 
  238. $str .= $this->getPostBody(); 
  239.  
  240. return $str;