[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/ -> AkHttpClient.php (source)

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


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6