7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Oauth_Http */
23 require_once 'Zend/Oauth/Http.php';
25 /** Zend_Oauth_Token_Request */
26 require_once 'Zend/Oauth/Token/Request.php';
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Oauth_Http_RequestToken extends Zend_Oauth_Http
37 * Singleton instance if required of the HTTP client
39 * @var Zend_Http_Client
41 protected $_httpClient = null;
44 * Initiate a HTTP request to retrieve a Request Token.
46 * @return Zend_Oauth_Token_Request
48 public function execute()
50 $params = $this->assembleParams();
51 $response = $this->startRequestCycle($params);
52 $return = new Zend_Oauth_Token_Request($response);
57 * Assemble all parameters for an OAuth Request Token request.
61 public function assembleParams()
64 'oauth_consumer_key' => $this->_consumer->getConsumerKey(),
65 'oauth_nonce' => $this->_httpUtility->generateNonce(),
66 'oauth_timestamp' => $this->_httpUtility->generateTimestamp(),
67 'oauth_signature_method' => $this->_consumer->getSignatureMethod(),
68 'oauth_version' => $this->_consumer->getVersion(),
71 // indicates we support 1.0a
72 if ($this->_consumer->getCallbackUrl()) {
73 $params['oauth_callback'] = $this->_consumer->getCallbackUrl();
75 $params['oauth_callback'] = 'oob';
78 if (!empty($this->_parameters)) {
79 $params = array_merge($params, $this->_parameters);
82 $params['oauth_signature'] = $this->_httpUtility->sign(
84 $this->_consumer->getSignatureMethod(),
85 $this->_consumer->getConsumerSecret(),
87 $this->_preferredRequestMethod,
88 $this->_consumer->getRequestTokenUrl()
95 * Generate and return a HTTP Client configured for the Header Request Scheme
96 * specified by OAuth, for use in requesting a Request Token.
98 * @param array $params
99 * @return Zend_Http_Client
101 public function getRequestSchemeHeaderClient(array $params)
103 $headerValue = $this->_httpUtility->toAuthorizationHeader(
106 $client = Zend_Oauth::getHttpClient();
107 $client->setUri($this->_consumer->getRequestTokenUrl());
108 $client->setHeaders('Authorization', $headerValue);
109 $rawdata = $this->_httpUtility->toEncodedQueryString($params, true);
110 if (!empty($rawdata)) {
111 $client->setRawData($rawdata);
113 $client->setMethod($this->_preferredRequestMethod);
118 * Generate and return a HTTP Client configured for the POST Body Request
119 * Scheme specified by OAuth, for use in requesting a Request Token.
121 * @param array $params
122 * @return Zend_Http_Client
124 public function getRequestSchemePostBodyClient(array $params)
126 $client = Zend_Oauth::getHttpClient();
127 $client->setUri($this->_consumer->getRequestTokenUrl());
128 $client->setMethod($this->_preferredRequestMethod);
130 $this->_httpUtility->toEncodedQueryString($params)
133 Zend_Http_Client::CONTENT_TYPE,
134 Zend_Http_Client::ENC_URLENCODED
140 * Attempt a request based on the current configured OAuth Request Scheme and
141 * return the resulting HTTP Response.
143 * @param array $params
144 * @return Zend_Http_Response
146 protected function _attemptRequest(array $params)
148 switch ($this->_preferredRequestScheme) {
149 case Zend_Oauth::REQUEST_SCHEME_HEADER:
150 $httpClient = $this->getRequestSchemeHeaderClient($params);
152 case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
153 $httpClient = $this->getRequestSchemePostBodyClient($params);
155 case Zend_Oauth::REQUEST_SCHEME_QUERYSTRING:
156 $httpClient = $this->getRequestSchemeQueryStringClient($params,
157 $this->_consumer->getRequestTokenUrl());
160 return $httpClient->request();