Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method that retrieves an oauth Request Token for 3-legged authentication #75

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
/vendor
/composer.lock
4 changes: 2 additions & 2 deletions examples/custom-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<title>Twitter retweets of me</title>

<ul>
<?php foreach ($statuses as $status): ?>
<?php foreach ($statuses as $status) { ?>
<li><a href="http://twitter.com/<?php echo $status->user->screen_name ?>"><img src="<?php echo htmlspecialchars($status->user->profile_image_url_https) ?>">
<?php echo htmlspecialchars($status->user->name) ?></a>:
<?php echo Twitter::clickable($status) ?>
<small>at <?php echo date('j.n.Y H:i', strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
<?php } ?>
</ul>
4 changes: 2 additions & 2 deletions examples/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
<title>Twitter timeline demo</title>

<ul>
<?php foreach ($statuses as $status): ?>
<?php foreach ($statuses as $status) { ?>
<li><a href="https://twitter.com/<?php echo $status->user->screen_name ?>"><img src="<?php echo htmlspecialchars($status->user->profile_image_url_https) ?>">
<?php echo htmlspecialchars($status->user->name) ?></a>:
<?php echo Twitter::clickable($status) ?>
<small>at <?php echo date('j.n.Y H:i', strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
<?php } ?>
</ul>
4 changes: 2 additions & 2 deletions examples/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<title>Twitter search demo</title>

<ul>
<?php foreach ($results as $status): ?>
<?php foreach ($results as $status) { ?>
<li><a href="https://twitter.com/<?php echo $status->user->screen_name ?>"><img src="<?php echo htmlspecialchars($status->user->profile_image_url_https) ?>">
<?php echo htmlspecialchars($status->user->name) ?></a>:
<?php echo Twitter::clickable($status) ?>
<small>at <?php echo date('j.n.Y H:i', strtotime($status->created_at)) ?></small>
</li>
<?php endforeach ?>
<?php } ?>
</ul>
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ if (!$twitter->authenticate()) {
}
```

The `getRequestToken()` method allows a Consumer application to obtain an OAuth Request Token to request user authorization:
- Documentation/Use-Cases: https://developer.twitter.com/en/docs/authentication/api-reference/request_token
- Define a Callback URL in your Twitter Application (Required): https://developer.twitter.com/en/docs/apps/callback-urls
```php
# You can Initialize a new Twitter object using only the Consumer Key and Secret
$this->twitter = new Twitter($consumerKey, $consumerSecret);
# Call the getRequestToken() using the callback URL defined in your twitter application.
$response = $this->twitter->getRequestToken('https://localhost.com/twitter-callback-url');
```

Example Response:

```php
{
"oauth_token": "x-oFdAAAAAABVLnXXXXXXXX_XXX",
"oauth_token_secret": "A810fifujUZXXXXXXXXXXXXXXXXXXXXX",
"oauth_callback_confirmed": "true"
}
```


Other commands
--------------

Expand Down
33 changes: 24 additions & 9 deletions src/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,17 @@ public function __construct(string $http_method, string $http_url, array $parame
/**
* attempt to build up a request from what was passed to the server
*/
public static function from_request(string $http_method = null, string $http_url = null, array $parameters = null): self
{
public static function from_request(
string $http_method = null,
string $http_url = null,
array $parameters = null
): self {
$scheme = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')
? 'http'
: 'https';
$http_url = ($http_url) ? $http_url : $scheme .
$http_url = ($http_url)
? $http_url
: $scheme .
'://' . $_SERVER['HTTP_HOST'] .
':' .
$_SERVER['SERVER_PORT'] .
Expand Down Expand Up @@ -339,7 +344,10 @@ public static function from_request(string $http_method = null, string $http_url

// We have a Authorization-header with OAuth data. Parse the header
// and add those overriding any duplicates from GET or POST
if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
if (
isset($request_headers['Authorization'])
&& substr($request_headers['Authorization'], 0, 6) == 'OAuth '
) {
$header_parameters = Util::split_header(
$request_headers['Authorization']
);
Expand All @@ -354,8 +362,13 @@ public static function from_request(string $http_method = null, string $http_url
/**
* pretty much a helper function to set up the request
*/
public static function from_consumer_and_token(Consumer $consumer, ?Token $token, string $http_method, string $http_url, array $parameters = null): self
{
public static function from_consumer_and_token(
Consumer $consumer,
?Token $token,
string $http_method,
string $http_url,
array $parameters = null
): self {
$parameters = $parameters ?: [];
$defaults = [
'oauth_version' => self::$version,
Expand Down Expand Up @@ -392,7 +405,7 @@ public function set_parameter(string $name, $value, bool $allow_duplicates = tru

public function get_parameter(string $name)
{
return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
return $this->parameters[$name] ?? null;
}


Expand Down Expand Up @@ -465,7 +478,9 @@ public function get_normalized_http_url(): string
$parts = parse_url($this->http_url);

$scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
$port = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80');
$port = (isset($parts['port']))
? $parts['port']
: (($scheme == 'https') ? '443' : '80');
$host = (isset($parts['host'])) ? $parts['host'] : '';
$path = (isset($parts['path'])) ? $parts['path'] : '';

Expand Down Expand Up @@ -581,7 +596,7 @@ class Util
public static function urlencode_rfc3986($input)
{
if (is_array($input)) {
return array_map([__CLASS__, 'urlencode_rfc3986'], $input);
return array_map([self::class, 'urlencode_rfc3986'], $input);
} elseif (is_scalar($input)) {
return str_replace('+', ' ', str_replace('%7E', '~', rawurlencode((string) $input)));
} else {
Expand Down
Loading