/Library/Kumbia/ServiceConsumer/Transport/Pecl_HTTP.php

https://github.com/loudertech/kef · PHP · 221 lines · 98 code · 18 blank · 105 comment · 16 complexity · 22780ec5cfab38202e7d2f6c3e3196ce MD5 · raw file

  1. <?php
  2. /**
  3. * Kumbia Enterprise Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the New BSD License that is bundled
  8. * with this package in the file docs/LICENSE.txt.
  9. *
  10. * If you did not receive a copy of the license and are unable to
  11. * obtain it through the world-wide-web, please send an email
  12. * to license@loudertechnology.com so we can send you a copy immediately.
  13. *
  14. * @category Kumbia
  15. * @category Kumbia
  16. * @package ServiceConsumer
  17. * @subpackage Transport
  18. * @copyright Copyright (c) 2008-2012 Louder Technology COL. (http://www.loudertechnology.com)
  19. * @license New BSD License
  20. * @version $Id: Sockets.php 122 2010-02-11 19:09:18Z gutierrezandresfelipe $
  21. */
  22. /**
  23. * Pecl_HTTPTransport
  24. *
  25. * Cliente para realizar peticiones HTTP usando la extensión de php pecl_http
  26. *
  27. * @category Kumbia
  28. * @package ServiceConsumer
  29. * @subpackage Transport
  30. * @copyright Copyright (c) 2008-2012 Louder Technology COL. (http://www.loudertechnology.com)
  31. * @license New BSD License
  32. * @abstract
  33. */
  34. class Pecl_HTTPTransport {
  35. /**
  36. * Host al que se le está realizando la petición
  37. *
  38. * @var string
  39. */
  40. private $_host;
  41. /**
  42. * Indica si se habilita el envío automático de las cookies recibidas
  43. *
  44. * @var boolean
  45. */
  46. private $_enableCookies = false;
  47. /**
  48. * Encabezados de la petición
  49. *
  50. * @var array
  51. */
  52. private $_headers = array();
  53. /**
  54. * Objeto de transporte HTTP
  55. *
  56. * @var HttpRequest
  57. */
  58. private $_transport;
  59. /**
  60. * Constructor del Pecl_HTTPCommunicator
  61. *
  62. * @param string $scheme
  63. * @param string $host
  64. * @param string $uri
  65. * @param string $method
  66. * @param int $port
  67. */
  68. public function __construct($scheme, $host, $uri, $method, $port=80){
  69. $url = $scheme.'://'.$host.'/'.$uri;
  70. if($method=='POST'){
  71. $this->_transport = new HttpRequest($url, HttpRequest::METH_POST);
  72. } else {
  73. $this->_transport = new HttpRequest($url, HttpRequest::METH_GET);
  74. }
  75. $this->_host = $host;
  76. }
  77. /**
  78. * Establece un encabezado HTTP a la petición
  79. *
  80. * @param string $name
  81. * @param string $value
  82. */
  83. public function addHeader($name, $value){
  84. $this->_headers[$name] = $value;
  85. }
  86. /**
  87. * Establece varios encabezados HTTP
  88. *
  89. * @param array $headers
  90. */
  91. public function setHeaders($headers){
  92. foreach($headers as $name => $value){
  93. $this->_headers[$name] = $value;
  94. }
  95. }
  96. /**
  97. * Changes request transaction
  98. *
  99. * @param string $url
  100. */
  101. public function setUrl($url){
  102. $this->_transport->setUrl($url);
  103. }
  104. /**
  105. * Establece el cuerpo HTTP de la petición
  106. *
  107. * @param string $rawBody
  108. */
  109. public function setRawPostData($rawBody){
  110. return $this->_transport->setBody($rawBody);
  111. }
  112. /**
  113. * Envía la petición
  114. *
  115. */
  116. public function send(){
  117. if($this->_enableCookies==true){
  118. if(isset($_SESSION['KHC'][$this->_host])){
  119. if(isset($_SESSION['KHC'][$this->_host]['time'])){
  120. if($_SESSION['KHC'][$this->_host]['time']>($_SERVER['REQUEST_TIME']-900)){
  121. $this->_transport->resetCookies();
  122. $this->_transport->setCookies($_SESSION['KHC'][$this->_host]['cookie']);
  123. } else {
  124. unset($_SESSION['KHC'][$this->_host]);
  125. }
  126. } else {
  127. unset($_SESSION['KHC'][$this->_host]);
  128. }
  129. }
  130. }
  131. $this->_transport->setOptions(array(
  132. 'timeout' => 900,
  133. 'connecttimeout' => 30,
  134. 'dns_cache_timeout' => 30
  135. ));
  136. $this->_transport->setHeaders($this->_headers);
  137. for($i=0;$i<3;$i++){
  138. try {
  139. $this->_transport->send();
  140. break;
  141. }
  142. catch(HttpInvalidParamException $e){
  143. if($i==2){
  144. throw new ServiceConsumerException($e->getMessage(), $e->getCode());
  145. }
  146. }
  147. }
  148. if($this->_enableCookies==true){
  149. if(!isset($_SESSION['KHC'][$this->_host])){
  150. $cookies = $this->_transport->getResponseCookies();
  151. if(count($cookies)){
  152. $_SESSION['KHC'][$this->_host] = array(
  153. 'time' => $_SERVER['REQUEST_TIME'],
  154. 'cookie' => $cookies[0]->cookies
  155. );
  156. }
  157. }
  158. }
  159. return true;
  160. }
  161. /**
  162. * Devuelve el código HTTP de la respuesta a la petición
  163. *
  164. * @return int
  165. */
  166. public function getResponseCode(){
  167. return $this->_transport->getResponseCode();
  168. }
  169. /**
  170. * Devuelve el cuerpo HTTP de la respuesta a la petición
  171. *
  172. * @return string
  173. */
  174. public function getResponseBody(){
  175. return $this->_transport->getResponseBody();
  176. }
  177. /**
  178. * Habilita el envio automático de las cookies recibidas
  179. *
  180. * @param boolean $enableCookies
  181. */
  182. public function enableCookies($enableCookies){
  183. $this->_enableCookies = $enableCookies;
  184. if($enableCookies==true){
  185. $this->_transport->enableCookies();
  186. }
  187. }
  188. /**
  189. * Graba en sesión las cookies de la petición
  190. *
  191. */
  192. public function getResponseCookies(){
  193. if($this->_enableCookies==true){
  194. $cookies = $this->_transport->getResponseCookies();
  195. if(count($cookies)){
  196. $_SESSION['KHC'][$this->_host] = array(
  197. 'time' => $_SERVER['REQUEST_TIME'],
  198. 'cookie' => $cookies[0]->cookies
  199. );
  200. }
  201. }
  202. }
  203. }