/** * This actually signs the request. * It can be called multiple times (for different requests) on the same instance * of the client. * This method puts it in the oauth params in the Authorization header by default. * * @param Request $request Request to sign * @throws Exception */ public function sign(Request $request) { $this->request = $request; $oauth_params = $this->getOAuthParams(); $oauth_params['oauth_signature'] = $this->getSignature(); //put it where it needs to go in the request switch ($this->config['signature_location']) { case self::SIGN_LOCATION_HEADER: //Needs escaping in the header, not in the QS $oauth_params['oauth_signature'] = Helpers::escape($oauth_params['oauth_signature']); $header = 'OAuth ' . Helpers::flattenAssocArray($oauth_params, '%s="%s"', ', '); $request->setHeader(Request::HEADER_AUTHORIZATION, $header); break; case self::SIGN_LOCATION_QUERY: foreach ($oauth_params as $param_name => $param_value) { $request->setParameter($param_name, $param_value); } break; default: throw new Exception('Invalid signing location specified.'); } //Reset so this instance of the Client can sign subsequent requests. //Mainly for the nonce. $this->resetOAuthParams(); }
public function addAttachment(Attachment $attachment, $include_online = false) { /** * @var Object $this */ $uri = sprintf('%s/%s/Attachments/%s', $this::getResourceURI(), $this->getGUID(), $attachment->getFileName()); $url = new URL($this->_application, $uri); $request = new Request($this->_application, $url, Request::METHOD_POST); if ($include_online) { $request->setParameter('IncludeOnline', 'true'); } $request->setBody($attachment->getContent(), $attachment->getMimeType()); $request->send(); $response = $request->getResponse(); if (false !== ($element = current($response->getElements()))) { $attachment->fromStringArray($element); } return $this; }
/** * @return array */ public function execute() { /** @var ObjectInterface $from_class */ $from_class = $this->from; $url = new URL($this->app, $from_class::getResourceURI()); $request = new Request($this->app, $url, Request::METHOD_GET); $where = $this->getWhere(); if (!empty($where)) { $request->setParameter('where', $where); } if ($this->order !== null) { $request->setParameter('order', $this->order); } if ($this->modifiedAfter !== null) { $request->setHeader('If-Modified-Since', $this->modifiedAfter); } if ($this->page !== null) { $request->setParameter('page', $this->page); } if ($this->offset !== null) { $request->setParameter('offset', $this->offset); } $request->send(); $elements = array(); foreach ($request->getResponse()->getElements() as $element) { /** @var Object $built_element */ $built_element = new $from_class($this->app); $built_element->fromStringArray($element); $elements[] = $built_element; } return $elements; }
/** * @param array $objects * @return null * @throws Exception */ public function saveAll(array $objects) { //Just get one type to compare with, doesn't matter which. $current_object = current($objects); $type = get_class($current_object); $has_guid = $current_object->hasGUID(); $object_arrays = array(); foreach ($objects as $object) { if ($type !== get_class($object)) { throw new Exception('Array passed to ->saveAll() must be homogeneous.'); } // Check if we have a GUID if ($object->hasGUID() && $has_guid === false) { $has_guid = true; } $object_arrays[] = $object->toStringArray(); } $request_method = $has_guid ? Request::METHOD_POST : Request::METHOD_PUT; $url = new URL($this, $type::getResourceURI()); $request = new Request($this, $url, $request_method); //This might need to be parsed and stored some day. $root_node_name = Helpers::pluralize($type::getRootNodeName()); $data = array($root_node_name => $object_arrays); $request->setBody(Helpers::arrayToXML($data)); $request->setParameter('SummarizeErrors', 'false'); $request->send(); $response = $request->getResponse(); foreach ($response->getElements() as $element_index => $element) { if ($response->getErrorsForElement($element_index) === null) { $objects[$element_index]->fromStringArray($element); $objects[$element_index]->setClean(); } } return $response; }
//Here's where you'll see if your keys are valid. You can catch a BadRequestException. try { $request->send(); } catch (Exception $e) { echo $e->getCode(); print_r($request->getResponse()->getOAuthResponse()); } $oauth_response = $request->getResponse()->getOAuthResponse(); setOAuthSession($oauth_response['oauth_token'], $oauth_response['oauth_token_secret']); printf('<a href="%s?oauth_token=%s">Click here to Authorize</a>', $xero->getAuthorizeURL(), $oauth_response['oauth_token']); exit; } elseif (isset($oauth_session['session_handle']) && !isset($oauth_session['expires'])) { // If session is expired refresh the token $url = new URL($xero, URL::OAUTH_ACCESS_TOKEN); $request = new Request($xero, $url); $request->setParameter('oauth_token', $oauth_session['token']); $request->setParameter('oauth_session_handle', $oauth_session['session_handle']); $request->send(); $oauth_response = $request->getResponse()->getOAuthResponse(); $expires = time() + intval($oauth_response['oauth_expires_in']); setOAuthSession($oauth_response['oauth_token'], $oauth_response['oauth_token_secret'], $expires, $oauth_response['oauth_session_handle']); $xero->getOAuthClient()->setToken($oauth_response['oauth_token'])->setTokenSecret($oauth_response['oauth_token_secret']); } else { $xero->getOAuthClient()->setToken($oauth_session['token'])->setTokenSecret($oauth_session['token_secret']); if (isset($_REQUEST['oauth_verifier'])) { $xero->getOAuthClient()->setVerifier($_REQUEST['oauth_verifier']); $url = new URL($xero, URL::OAUTH_ACCESS_TOKEN); $request = new Request($xero, $url); $request->send(); $oauth_response = $request->getResponse()->getOAuthResponse(); $expires = time() + intval($oauth_response['oauth_expires_in']);