[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/Net/ -> Client.php (source)

   1  <?php
   2  /*+***********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *************************************************************************************/
  10  include  'vtlib/thirdparty/network/Request.php';
  11  
  12  /**
  13   * Provides API to work with HTTP Connection.
  14   * @package vtlib
  15   */
  16  class Vtiger_Net_Client {
  17      var $client;
  18      var $url;
  19      var $response;
  20  
  21      /**
  22       * Constructor
  23       * @param String URL of the site
  24       * Example: 
  25       * $client = new Vtiger_New_Client('http://www.vtiger.com');
  26       */
  27  	function __construct($url) {
  28          $this->setURL($url);
  29      }
  30  
  31      /**
  32       * Set another url for this instance
  33       * @param String URL to use go forward
  34       */
  35  	function setURL($url) {
  36          $this->url = $url;
  37          $this->client = new HTTP_Request();
  38          $this->response = false;
  39          $this->setDefaultHeaders();
  40      }
  41      
  42  	function setDefaultHeaders() {
  43          $headers = array();
  44          if (isset($_SERVER)) {
  45              global $site_URL;
  46              $headers['referer'] = isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER'] : ($site_URL . "?noreferer");
  47              
  48              if (isset($_SERVER['HTTP_USER_AGENT'])) {
  49                  $headers['user-agent'] = $_SERVER['HTTP_USER_AGENT'];
  50              }
  51              
  52          } else {
  53              global $site_URL;
  54              $headers['referer'] = ($site_URL . "?noreferer");
  55          }
  56          
  57          $this->setHeaders($headers);
  58      }
  59  
  60      /**
  61       * Set custom HTTP Headers
  62       * @param Map HTTP Header and Value Pairs
  63       */
  64  	function setHeaders($values) {
  65          foreach($values as $key=>$value) {
  66              $this->client->addHeader($key, $value);
  67          }
  68      }
  69      
  70      /**
  71       * Perform a GET request
  72       * @param Map key-value pair or false
  73       * @param Integer timeout value
  74       */
  75  	function doGet($params=false, $timeout=null) {
  76          if($timeout) $this->client->_timeout = $timeout;
  77          $this->client->setURL($this->url);
  78          $this->client->setMethod(HTTP_REQUEST_METHOD_GET);
  79  
  80          if($params) {
  81              foreach($params as $key=>$value) 
  82                  $this->client->addQueryString($key, $value);
  83          }
  84          $this->response = $this->client->sendRequest();
  85  
  86          $content = false;
  87          if(!$this->wasError()) {
  88              $content = $this->client->getResponseBody();
  89          }
  90          $this->disconnect();
  91          return $content;
  92      }
  93  
  94      /**
  95       * Perform a POST request
  96       * @param Map key-value pair or false
  97       * @param Integer timeout value
  98       */
  99  	function doPost($params=false, $timeout=null) {
 100          if($timeout) $this->client->_timeout = $timeout;
 101          $this->client->setURL($this->url);
 102          $this->client->setMethod(HTTP_REQUEST_METHOD_POST);
 103  
 104          if($params) {
 105              if(is_string($params)) $this->client->addRawPostData($params);
 106              else {
 107                  foreach($params as $key=>$value)
 108                      $this->client->addPostData($key, $value);
 109              }
 110          }
 111          $this->response = $this->client->sendRequest();
 112  
 113          $content = false;
 114          if(!$this->wasError()) {
 115              $content = $this->client->getResponseBody();
 116          }
 117          $this->disconnect();
 118  
 119          return $content;
 120      }
 121  
 122      /**
 123       * Did last request resulted in error?
 124       */
 125  	function wasError() {
 126          return PEAR::isError($this->response);
 127      }
 128  
 129      /**
 130       * Disconnect this instance
 131       */
 132  	function disconnect() {
 133          $this->client->disconnect();
 134      }
 135  }
 136  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1