/branches/lazy-helpers/lib/AkHttpClient.php

https://github.com/akelos/v1 · PHP · 215 lines · 173 code · 41 blank · 1 comment · 19 complexity · a2f1a2f50e0a889a00a0c98a82d06f77 MD5 · raw file

  1. <?php
  2. class AkHttpClient extends AkObject
  3. {
  4. var $HttpRequest;
  5. var $error;
  6. var $Response;
  7. function get($url, $options = array())
  8. {
  9. return $this->customRequest($url, 'GET', $options);
  10. }
  11. function post($url, $options = array(), $body = '')
  12. {
  13. return $this->customRequest($url, 'POST', $options, $body);
  14. }
  15. function put($url, $options = array(), $body = '')
  16. {
  17. return $this->customRequest($url, 'PUT', $options, $body);
  18. }
  19. function delete($url, $options = array())
  20. {
  21. return $this->customRequest($url, 'DELETE', $options);
  22. }
  23. // prefix_options, query_options = split_options(options)
  24. function customRequest($url, $http_verb = 'GET', $options = array(), $body = '')
  25. {
  26. $this->getRequestInstance($url, $http_verb, $options, $body);
  27. return empty($options['cache']) ? $this->sendRequest() : $this->returnCustomRequestFromCache($url,$options);
  28. }
  29. function returnCustomRequestFromCache($url, $options)
  30. {
  31. $Cache = Ak::cache();
  32. $Cache->init(is_numeric($options['cache']) ? $options['cache'] : 86400, !isset($options['cache_type']) ? 1 : $options['cache_type']);
  33. if (!$data = $Cache->get('AkHttpClient_'.md5($url))) {
  34. $data = $this->sendRequest();
  35. $Cache->save($data);
  36. }
  37. return $data;
  38. }
  39. function urlExists($url)
  40. {
  41. $this->getRequestInstance($url, 'GET');
  42. $this->sendRequest(false);
  43. return $this->code == 200;
  44. }
  45. function getRequestInstance($url, $http_verb = 'GET', $options = array(), $body = '')
  46. {
  47. $default_options = array(
  48. 'header' => array(),
  49. 'params' => array(),
  50. );
  51. $options = array_merge($default_options, $options);
  52. $options['header']['user-agent'] = empty($options['header']['user-agent']) ?
  53. 'Akelos PHP Framework AkHttpClient (http://akelos.org)' : $options['header']['user-agent'];
  54. list($user_name, $password) = $this->_extractUserNameAndPasswordFromUrl($url);
  55. require_once(AK_VENDOR_DIR.DS.'pear'.DS.'HTTP'.DS.'Request.php');
  56. $this->{'_setParamsFor'.ucfirst(strtolower($http_verb))}($url, $options['params']);
  57. $this->HttpRequest =& new HTTP_Request($url);
  58. $user_name ? $this->HttpRequest->setBasicAuth($user_name, $password) : null;
  59. $this->HttpRequest->setMethod(constant('HTTP_REQUEST_METHOD_'.$http_verb));
  60. if(!empty($body)){
  61. $this->setBody($body);
  62. }elseif ($http_verb == 'PUT' && !empty($options['params'])){
  63. $this->setBody($options['params']);
  64. }
  65. !empty($options['params']) && $this->addParams($options['params']);
  66. $this->addHeaders($options['header']);
  67. return $this->HttpRequest;
  68. }
  69. function addHeaders($headers)
  70. {
  71. foreach ($headers as $k=>$v){
  72. $this->addHeader($k, $v);
  73. }
  74. }
  75. function addHeader($name, $value)
  76. {
  77. $this->HttpRequest->removeHeader($name);
  78. $this->HttpRequest->addHeader($name, $value);
  79. }
  80. function getResponseHeader($name)
  81. {
  82. return $this->HttpRequest->getResponseHeader($name);
  83. }
  84. function getResponseHeaders()
  85. {
  86. return $this->HttpRequest->getResponseHeader();
  87. }
  88. function getResponseCode()
  89. {
  90. return $this->HttpRequest->getResponseCode();
  91. }
  92. function addParams($params = array())
  93. {
  94. if(!empty($params)){
  95. foreach (array_keys($params) as $k){
  96. $this->HttpRequest->addPostData($k, $params[$k]);
  97. }
  98. }
  99. }
  100. function setBody($body)
  101. {
  102. Ak::compat('http_build_query');
  103. $this->HttpRequest->setBody(http_build_query((array)$body));
  104. }
  105. function sendRequest($return_body = true)
  106. {
  107. $this->Response = $this->HttpRequest->sendRequest();
  108. $this->code = $this->HttpRequest->getResponseCode();
  109. if (PEAR::isError($this->Response)) {
  110. $this->error = $this->Response->getMessage();
  111. return false;
  112. } else {
  113. return $return_body ? $this->HttpRequest->getResponseBody() : true;
  114. }
  115. }
  116. function _extractUserNameAndPasswordFromUrl(&$url)
  117. {
  118. return array(null,null);
  119. }
  120. function getParamsOnUrl($url)
  121. {
  122. $parts = parse_url($url);
  123. if($_tmp = (empty($parts['query']) ? false : $parts['query'])){
  124. unset($parts['query']);
  125. $url = $this->_httpRenderQuery($parts);
  126. }
  127. $result = array();
  128. !empty($_tmp) && parse_str($_tmp, $result);
  129. return $result;
  130. }
  131. function getUrlWithParams($url, $params)
  132. {
  133. $parts = parse_url($url);
  134. Ak::compat('http_build_query');
  135. $parts['query'] = http_build_query($params);
  136. return $this->_httpRenderQuery($parts);
  137. }
  138. function _setParamsForGet(&$url, &$params)
  139. {
  140. $url_params = $this->getParamsOnUrl($url);
  141. if(!count($url_params) && !empty($params)){
  142. $url = $this->getUrlWithParams($url, $params);
  143. }else{
  144. $params = $url_params;
  145. }
  146. }
  147. function _setParamsForPost(&$url, &$params)
  148. {
  149. empty($params) && $params = $this->getParamsOnUrl($url);
  150. }
  151. function _setParamsForPut(&$url, &$params)
  152. {
  153. empty($params) && $params = $this->getParamsOnUrl($url);
  154. }
  155. function _setParamsForDelete(&$url, &$params)
  156. {
  157. if(!$this->getParamsOnUrl($url) && !empty($params)){
  158. $url = $this->getUrlWithParams($url, $params);
  159. }
  160. }
  161. function _httpRenderQuery($parts)
  162. {
  163. return is_array($parts) ? (
  164. (isset($parts['scheme']) ? $parts['scheme'].':'.((strtolower($parts['scheme']) == 'mailto') ? '' : '//') : '').
  165. (isset($parts['user']) ? $parts['user'].(isset($parts['pass']) ? ':'.$parts['pass'] : '').'@' : '').
  166. (isset($parts['host']) ? $parts['host'] : '').
  167. (isset($parts['port']) ? ':'.$parts['port'] : '').
  168. (isset($parts['path'])?((substr($parts['path'], 0, 1) == '/') ? $parts['path'] : ('/'.$parts['path'])):'').
  169. (isset($parts['query']) ? '?'.$parts['query'] : '').
  170. (isset($parts['fragment']) ? '#'.$parts['fragment'] : '')
  171. ) : false;
  172. }
  173. }
  174. ?>