Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Http.php
Go to the documentation of this file.
1 <?php
23 #require_once 'Zend/Oauth/Http/Utility.php';
24 
26 #require_once 'Zend/Uri/Http.php';
27 
35 {
42  protected $_parameters = array();
43 
49  protected $_consumer = null;
50 
58  protected $_preferredRequestScheme = null;
59 
66 
72  protected $_httpUtility = null;
73 
82  public function __construct(
83  Zend_Oauth_Consumer $consumer,
84  array $parameters = null,
85  Zend_Oauth_Http_Utility $utility = null
86  ) {
87  $this->_consumer = $consumer;
88  $this->_preferredRequestScheme = $this->_consumer->getRequestScheme();
89  if ($parameters !== null) {
90  $this->setParameters($parameters);
91  }
92  if ($utility !== null) {
93  $this->_httpUtility = $utility;
94  } else {
95  $this->_httpUtility = new Zend_Oauth_Http_Utility;
96  }
97  }
98 
105  public function setMethod($method)
106  {
107  if (!in_array($method, array(Zend_Oauth::POST, Zend_Oauth::GET))) {
108  #require_once 'Zend/Oauth/Exception.php';
109  throw new Zend_Oauth_Exception('invalid HTTP method: ' . $method);
110  }
111  $this->_preferredRequestMethod = $method;
112  return $this;
113  }
114 
120  public function getMethod()
121  {
123  }
124 
131  public function setParameters(array $customServiceParameters)
132  {
133  $this->_parameters = $customServiceParameters;
134  return $this;
135  }
136 
142  public function getParameters()
143  {
144  return $this->_parameters;
145  }
146 
152  public function getConsumer()
153  {
154  return $this->_consumer;
155  }
156 
169  public function startRequestCycle(array $params)
170  {
171  $response = null;
172  $body = null;
173  $status = null;
174  try {
175  $response = $this->_attemptRequest($params);
176  } catch (Zend_Http_Client_Exception $e) {
177  #require_once 'Zend/Oauth/Exception.php';
178  throw new Zend_Oauth_Exception('Error in HTTP request', null, $e);
179  }
180  if ($response !== null) {
181  $body = $response->getBody();
182  $status = $response->getStatus();
183  }
184  if ($response === null // Request failure/exception
185  || $status == 500 // Internal Server Error
186  || $status == 400 // Bad Request
187  || $status == 401 // Unauthorized
188  || empty($body) // Missing token
189  ) {
191  $response = $this->startRequestCycle($params);
192  }
193  return $response;
194  }
195 
205  {
206  $client = Zend_Oauth::getHttpClient();
207  $client->setUri($url);
208  $client->getUri()->setQuery(
209  $this->_httpUtility->toEncodedQueryString($params)
210  );
211  $client->setMethod($this->_preferredRequestMethod);
212  return $client;
213  }
214 
224  {
225  switch ($this->_preferredRequestScheme) {
227  $this->_preferredRequestScheme = Zend_Oauth::REQUEST_SCHEME_POSTBODY;
228  break;
230  $this->_preferredRequestScheme = Zend_Oauth::REQUEST_SCHEME_QUERYSTRING;
231  break;
232  default:
233  #require_once 'Zend/Oauth/Exception.php';
234  throw new Zend_Oauth_Exception(
235  'Could not retrieve a valid Token response from Token URL:'
236  . ($response !== null
237  ? PHP_EOL . $response->getBody()
238  : ' No body - check for headers')
239  );
240  }
241  }
242 
251  protected function _toAuthorizationHeader(array $params, $realm = null)
252  {
253  $headerValue = array();
254  $headerValue[] = 'OAuth realm="' . $realm . '"';
255  foreach ($params as $key => $value) {
256  if (!preg_match("/^oauth_/", $key)) {
257  continue;
258  }
259  $headerValue[] = Zend_Oauth_Http_Utility::urlEncode($key)
260  . '="'
262  . '"';
263  }
264  return implode(",", $headerValue);
265  }
266 }
$_preferredRequestScheme
Definition: Http.php:58
static urlEncode($value)
Definition: Utility.php:211
$response
Definition: 404.php:11
$_preferredRequestMethod
Definition: Http.php:65
const REQUEST_SCHEME_HEADER
Definition: Oauth.php:33
setMethod($method)
Definition: Http.php:105
const POST
Definition: Oauth.php:37
getRequestSchemeQueryStringClient(array $params, $url)
Definition: Http.php:204
setParameters(array $customServiceParameters)
Definition: Http.php:131
$value
Definition: gender.phtml:16
const REQUEST_SCHEME_QUERYSTRING
Definition: Oauth.php:35
static getHttpClient()
Definition: Oauth.php:69
_assessRequestAttempt(Zend_Http_Response $response=null)
Definition: Http.php:223
startRequestCycle(array $params)
Definition: Http.php:169
$status
Definition: order_status.php:8
$method
Definition: info.phtml:13
const GET
Definition: Oauth.php:36
__construct(Zend_Oauth_Consumer $consumer, array $parameters=null, Zend_Oauth_Http_Utility $utility=null)
Definition: Http.php:82
_toAuthorizationHeader(array $params, $realm=null)
Definition: Http.php:251
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
const REQUEST_SCHEME_POSTBODY
Definition: Oauth.php:34