4 * Copyright (c) 2007 Andy Smith
6 namespace Abraham\TwitterOAuth;
10 protected $parameters;
11 protected $httpMethod;
13 public static $version = '1.0';
18 * @param string $httpMethod
19 * @param string $httpUrl
20 * @param array|null $parameters
22 public function __construct($httpMethod, $httpUrl, array $parameters = [])
24 $parameters = array_merge(Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)), $parameters);
25 $this->parameters = $parameters;
26 $this->httpMethod = $httpMethod;
27 $this->httpUrl = $httpUrl;
31 * pretty much a helper function to set up the request
33 * @param Consumer $consumer
35 * @param string $httpMethod
36 * @param string $httpUrl
37 * @param array $parameters
41 public static function fromConsumerAndToken(
46 array $parameters = []
49 "oauth_version" => Request::$version,
50 "oauth_nonce" => Request::generateNonce(),
51 "oauth_timestamp" => time(),
52 "oauth_consumer_key" => $consumer->key
54 if (null !== $token) {
55 $defaults['oauth_token'] = $token->key;
58 $parameters = array_merge($defaults, $parameters);
60 return new Request($httpMethod, $httpUrl, $parameters);
65 * @param string $value
67 public function setParameter($name, $value)
69 $this->parameters[$name] = $value;
77 public function getParameter($name)
79 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
85 public function getParameters()
87 return $this->parameters;
93 public function removeParameter($name)
95 unset($this->parameters[$name]);
99 * The request parameters, sorted and concatenated into a normalized string.
103 public function getSignableParameters()
105 // Grab all parameters
106 $params = $this->parameters;
108 // Remove oauth_signature if present
109 // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
110 if (isset($params['oauth_signature'])) {
111 unset($params['oauth_signature']);
114 return Util::buildHttpQuery($params);
118 * Returns the base string of this request
120 * The base string defined as the method, the url
121 * and the parameters (normalized), each urlencoded
122 * and the concated with &.
126 public function getSignatureBaseString()
129 $this->getNormalizedHttpMethod(),
130 $this->getNormalizedHttpUrl(),
131 $this->getSignableParameters()
134 $parts = Util::urlencodeRfc3986($parts);
136 return implode('&', $parts);
140 * Returns the HTTP Method in uppercase
144 public function getNormalizedHttpMethod()
146 return strtoupper($this->httpMethod);
150 * parses the url and rebuilds it to be
155 public function getNormalizedHttpUrl()
157 $parts = parse_url($this->httpUrl);
159 $scheme = $parts['scheme'];
160 $host = strtolower($parts['host']);
161 $path = $parts['path'];
163 return "$scheme://$host$path";
167 * Builds a url usable for a GET request
171 public function toUrl()
173 $postData = $this->toPostdata();
174 $out = $this->getNormalizedHttpUrl();
176 $out .= '?' . $postData;
182 * Builds the data one would send in a POST request
186 public function toPostdata()
188 return Util::buildHttpQuery($this->parameters);
192 * Builds the Authorization: header
195 * @throws TwitterOAuthException
197 public function toHeader()
200 $out = 'Authorization: OAuth';
201 foreach ($this->parameters as $k => $v) {
202 if (substr($k, 0, 5) != "oauth") {
206 throw new TwitterOAuthException('Arrays not supported in headers');
208 $out .= ($first) ? ' ' : ', ';
209 $out .= Util::urlencodeRfc3986($k) . '="' . Util::urlencodeRfc3986($v) . '"';
218 public function __toString()
220 return $this->toUrl();
224 * @param SignatureMethod $signatureMethod
225 * @param Consumer $consumer
226 * @param Token $token
228 public function signRequest(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null)
230 $this->setParameter("oauth_signature_method", $signatureMethod->getName());
231 $signature = $this->buildSignature($signatureMethod, $consumer, $token);
232 $this->setParameter("oauth_signature", $signature);
236 * @param SignatureMethod $signatureMethod
237 * @param Consumer $consumer
238 * @param Token $token
242 public function buildSignature(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null)
244 return $signatureMethod->buildSignature($this, $consumer, $token);
250 public static function generateNonce()
252 return md5(microtime() . mt_rand());