/** * Send a POST request to the specified URL with the specified payload. * @param string $url * @param string $data * @return string Remote data **/ public function sendPOST($url, $data = array()) { $data['_fake_status'] = '200'; // Send the actual request. $this->instance->setHeader('User-Agent', sprintf(Imgur::$user_agent, Imgur::$key)); $this->instance->setMethod('POST'); $this->instance->setUrl($url); foreach ($data as $k => $v) { $this->instance->addPostParameter($k, $v); } try { /** @var HTTP_Request2_Response */ $response = $this->instance->send(); return $response->getBody(); } catch (HTTP_Request2_Exception $e) { throw new Imgur_Exception("HTTP Request Failure", null, $e); } catch (Exception $e) { throw new Imgur_Exception("Unknown Failure during HTTP Request", null, $e); } }
/** * Fetches a secured oauth url and returns the response body. * * @param string $uri * @param mixed $arguments * @param string $method * @param array $httpHeaders * @return string */ public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) { $consumerRequest = new HTTP_OAuth_Consumer_Request(); $consumerRequest->setUrl($uri); $consumerRequest->setMethod($method); $consumerRequest->setSecrets($this->OAuth->getSecrets()); $parameters = array('oauth_consumer_key' => $this->consumerKey, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $this->oauth_token); if (is_array($arguments)) { $parameters = array_merge($parameters, $arguments); } elseif (is_string($arguments)) { $consumerRequest->setBody($arguments); } $consumerRequest->setParameters($parameters); if (count($httpHeaders)) { foreach ($httpHeaders as $k => $v) { $consumerRequest->setHeader($k, $v); } } $response = $consumerRequest->send(); switch ($response->getStatus()) { // Not modified case 304: return array('httpStatus' => 304, 'body' => null); break; case 403: throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.'); case 404: throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found'); case 507: throw new Dropbox_Exception_OverQuota('This dropbox is full'); } $return = array('httpStatus' => $response->getStatus(), 'body' => $response->getBody()); // Debugging: //error_log( print_r( $return, true ) ); return $return; }
/** * Fetches a secured oauth url and returns the response body. * * @param string $uri * @param mixed $arguments * @param string $method * @param array $httpHeaders * @return string */ public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) { $httpRequest = new HTTP_Request2(null, HTTP_Request2::METHOD_GET, array('ssl_verify_peer' => false, 'ssl_verify_host' => false)); $consumerRequest = new HTTP_OAuth_Consumer_Request(); $consumerRequest->accept($httpRequest); $consumerRequest->setUrl($uri); $consumerRequest->setMethod($method); $consumerRequest->setSecrets($this->OAuth->getSecrets()); $parameters = array('oauth_consumer_key' => $this->consumerKey, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_token' => $this->oauth_token); if (is_array($arguments)) { $parameters = array_merge($parameters, $arguments); } elseif (is_string($arguments)) { $consumerRequest->setBody($arguments); } $consumerRequest->setParameters($parameters); if (count($httpHeaders)) { foreach ($httpHeaders as $k => $v) { $consumerRequest->setHeader($k, $v); } } $response = $consumerRequest->send(); switch ($response->getStatus()) { // Not modified case 304: return array('httpStatus' => 304, 'body' => null); break; case 400: throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.'); case 401: throw new Dropbox_Exception_Forbidden('Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.'); case 403: throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.'); case 404: throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found'); case 405: throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).'); case 500: throw new Dropbox_Exception_Forbidden('Server error. ' . $e->getMessage()); case 503: throw new Dropbox_Exception_Forbidden('Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.'); case 507: throw new Dropbox_Exception_OverQuota('This dropbox is full'); } return array('httpStatus' => $response->getStatus(), 'body' => $response->getBody()); }
public function testNoOAuthParametersInGET() { $mockReq = new HTTP_Request2('http://example.com'); $mockReq->setAdapter(new HTTP_Request2_Adapter_Mock()); $req = new HTTP_OAuth_Consumer_Request(); $req->accept($mockReq); $req->foo = 'bar'; $req->oauth_consumer_key = 'key'; $req->send(); $this->assertEquals('http://example.com/?foo=bar', $req->getUrl()->getUrl()); }