/OAuth2/HttpClient.php

https://github.com/rithis-archive/oauth2 · PHP · 180 lines · 70 code · 23 blank · 87 comment · 5 complexity · 3ebc2b5dd8f0c885e1482bc31eb0253a MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2010 VZnet Netzwerke Ltd.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * @author Bastian Hofmann <bhfomann@vz.net>
  24. * @author Vyacheslav Slinko <vyacheslav.slinko@gmail.com>
  25. * @copyright 2010 VZnet Netzwerke Ltd.
  26. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  27. */
  28. namespace OAuth2;
  29. class HttpClient
  30. {
  31. /**
  32. * @var string
  33. */
  34. private $_url;
  35. /**
  36. * @var string
  37. */
  38. private $_method;
  39. /**
  40. * @var string
  41. */
  42. private $_parameters;
  43. /**
  44. * @var array
  45. */
  46. private $_requestHeader;
  47. /**
  48. * @var string
  49. */
  50. private $_response;
  51. /**
  52. * @var array
  53. */
  54. private $_headers;
  55. /**
  56. * @var array
  57. */
  58. private $_info;
  59. /**
  60. * @var boolean
  61. */
  62. private $_debug = false;
  63. /**
  64. * @param string $url
  65. * @param string $method
  66. * @param string $parameters
  67. * @param array $header any additional header which should be set
  68. */
  69. public function __construct($url, $method, $parameters = null, array $header = array()) {
  70. $this->_url = $url;
  71. $this->_method = $method;
  72. $this->_parameters = $parameters;
  73. $this->_requestHeader = $header;
  74. }
  75. /**
  76. * parses a string with two delimiters to an array
  77. *
  78. * example:
  79. *
  80. * param1=value1&param2=value2
  81. *
  82. * will result with delimiters & and = to
  83. *
  84. * array(
  85. * 'param1' => 'value1',
  86. * 'param2' => 'value2',
  87. * )
  88. *
  89. * @param string $string
  90. * @param string $firstDelimiter
  91. * @param string $secondDelimiter
  92. * @return array
  93. */
  94. public static function parseStringToArray($string, $firstDelimiter, $secondDelimiter) {
  95. $resultArray = array();
  96. $parts = explode($firstDelimiter, $string);
  97. foreach ($parts as $part) {
  98. $partsPart = explode($secondDelimiter, $part);
  99. $resultArray[$partsPart[0]] = isset($partsPart[1]) ? trim($partsPart[1]) : '';
  100. }
  101. return $resultArray;
  102. }
  103. /**
  104. * executes the curl request
  105. */
  106. public function execute() {
  107. $ch = curl_init();
  108. if ($this->_method === 'POST') {
  109. curl_setopt($ch, CURLOPT_URL, $this->_url);
  110. curl_setopt($ch, CURLOPT_POST, 1);
  111. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_parameters);
  112. } else {
  113. curl_setopt($ch, CURLOPT_URL, $this->_url . ($this->_parameters ? '?' . $this->_parameters : ''));
  114. }
  115. curl_setopt($ch, CURLOPT_HEADER, 1);
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  117. if (! empty($this->_requestHeader)) {
  118. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_requestHeader);
  119. }
  120. $fullResponse = curl_exec($ch);
  121. $this->_info = curl_getinfo($ch);
  122. $this->_response = substr($fullResponse, $this->_info['header_size'], strlen($fullResponse));
  123. if ($this->_response === false) {
  124. $this->_response = '';
  125. }
  126. $headers = rtrim(substr($fullResponse, 0, $this->_info['header_size']));
  127. $this->_headers = static::parseStringToArray($headers, PHP_EOL, ':');
  128. if ($this->_debug) {
  129. echo "<pre>";
  130. print_r($this->_url);
  131. echo PHP_EOL;
  132. print_r($this->_headers);
  133. echo PHP_EOL;
  134. print_r($this->_response);
  135. echo "</pre>";
  136. }
  137. curl_close($ch);
  138. }
  139. /**
  140. * @return string
  141. */
  142. public function getResponse() {
  143. return $this->_response;
  144. }
  145. /**
  146. * @return array
  147. */
  148. public function getHeaders() {
  149. return $this->_headers;
  150. }
  151. /**
  152. * @param boolean $debug
  153. */
  154. public function setDebug($debug) {
  155. $this->_debug = $debug;
  156. }
  157. }