public function index() { $HttpSocket = new HttpSocket (); if (sizeof ( $this->params ['pass'] [0] ) != 1) { throw new InvalidArgumentException (); } if (! empty ( $_SERVER ['PHP_AUTH_USER'] ) && ! empty ( $_SERVER ['PHP_AUTH_PW'] )) { $HttpSocket->configAuth ( 'Basic', $_SERVER ['PHP_AUTH_USER'], $_SERVER ['PHP_AUTH_PW'] ); } elseif (isset ( $this->CurrentUser )) { $HttpSocket->configAuth ( 'Basic', $this->Session->read ( 'Auth.User.Login' ), $this->Session->read ( 'Auth.User.Password' ) ); } $this->response->type ( 'json' ); $request = array ( 'method' => env ( 'REQUEST_METHOD' ), 'body' => $this->request->data, 'uri' => array ( 'scheme' => Configure::read ( 'Api.scheme' ), 'host' => Configure::read ( 'Api.host' ), 'port' => 80, 'path' => Configure::read ( 'Api.path' ) . $this->params ['pass'] [0], 'query' => $this->params->query ) ); $response = $HttpSocket->request ( $request ); $this->response->statusCode ( $response->code ); $this->response->body ( $response->body ); return $this->response; }
/** * Submits a request to Stripe. Requests are merged with default values, such as * the api host. If an error occurs, it is stored in `$lastError` and `false` is * returned. * * @param array $request Request details * @return mixed `false` on failure, data on success */ public function request($request = array()) { $this->lastError = null; $this->request = array('uri' => array('host' => 'api.stripe.com', 'scheme' => 'https', 'path' => '/'), 'method' => 'GET'); $this->request = Set::merge($this->request, $request); $this->request['uri']['path'] = '/v1/' . trim($this->request['uri']['path'], '/'); $this->Http->configAuth('Basic', $this->config['api_key'], ''); try { $response = $this->Http->request($this->request); switch ($this->Http->response['status']['code']) { case '200': return json_decode($response, true); break; case '400': $error = json_decode($response, true); $this->lastError = $error['error']['message']; return false; break; case '402': $error = json_decode($response, true); $this->lastError = $error['error']['message']; return false; break; default: $this->lastError = 'Unexpected error.'; CakeLog::write('stripe', $this->lastError); return false; break; } } catch (CakeException $e) { $this->lastError = $e->message; CakeLog::write('stripe', $e->message); } }
protected function _request($method, $params = array(), $request = array()) { // preparing request $query = Hash::merge(array('method' => $method, 'format' => 'json'), $params); $request = Hash::merge($this->_request, array('uri' => array('query' => $query)), $request); // Read cached GET results if ($request['method'] == 'GET') { $cacheKey = $this->_generateCacheKey(); $results = Cache::read($cacheKey); if ($results !== false) { return $results; } } // createding http socket object with auth configuration $HttpSocket = new HttpSocket(); $HttpSocket->configAuth('OauthLib.Oauth', array('Consumer' => array('consumer_token' => $this->_config['key'], 'consumer_secret' => $this->_config['secret']), 'Token' => array('token' => $this->_config['token'], 'secret' => $this->_config['secret2']))); // issuing request $response = $HttpSocket->request($request); // olny valid response is going to be parsed if ($response->code != 200) { if (Configure::read('debugApis')) { debug($request); debug($response->body); } return false; } // parsing response $results = $this->_parseResponse($response); // cache and return results if ($request['method'] == 'GET') { Cache::write($cacheKey, $results); } return $results; }
/** * Retrieve information about the authentication * * @param HttpSocket $http Http socket instance. * @param array &$authInfo Authentication info. * @return bool */ protected static function _getServerInformation(HttpSocket $http, &$authInfo) { $originalRequest = $http->request; $http->configAuth(false); $http->request($http->request); $http->request = $originalRequest; $http->configAuth('Digest', $authInfo); if (empty($http->response['header']['WWW-Authenticate'])) { return false; } preg_match_all('@(\\w+)=(?:(?:")([^"]+)"|([^\\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER); foreach ($matches as $match) { $authInfo[$match[1]] = $match[2]; } if (!empty($authInfo['qop']) && empty($authInfo['nc'])) { $authInfo['nc'] = 1; } return true; }