You are here

public function TwitterAPIExchange::buildOauth in Twitter Profile Widget 8.2

Same name and namespace in other branches
  1. 8 src/Resources/j7mbo/twitter_api_php/TwitterAPIExchange.php \Drupal\twitter_profile_widget\Resources\j7mbo\twitter_api_php\TwitterAPIExchange::buildOauth()

Build the Oauth object using params set in construct and additionals passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1

Parameters

string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json:

string $requestMethod Either POST or GET:

Return value

\TwitterAPIExchange Instance of self for method chaining

Throws

\Exception

1 call to TwitterAPIExchange::buildOauth()
TwitterAPIExchange::setPostfields in src/Resources/j7mbo/twitter_api_php/TwitterAPIExchange.php
Set postfields array, example: ['screen_name' => 'J7mbo']

File

src/Resources/j7mbo/twitter_api_php/TwitterAPIExchange.php, line 206

Class

TwitterAPIExchange
Twitter-API-PHP : Simple PHP wrapper for the v1.1 API

Namespace

Drupal\twitter_profile_widget\Resources\j7mbo\twitter_api_php

Code

public function buildOauth($url, $requestMethod) {
  if (!in_array(strtolower($requestMethod), [
    'post',
    'get',
  ])) {
    throw new Exception('Request method must be either POST or GET');
  }
  $consumer_key = $this->consumer_key;
  $consumer_secret = $this->consumer_secret;
  $oauth_access_token = $this->oauth_access_token;
  $oauth_access_token_secret = $this->oauth_access_token_secret;
  $oauth = [
    'oauth_consumer_key' => $consumer_key,
    'oauth_nonce' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_token' => $oauth_access_token,
    'oauth_timestamp' => time(),
    'oauth_version' => '1.0',
  ];
  $getfield = $this
    ->getGetfield();
  if (!is_null($getfield)) {
    $getfields = str_replace('?', '', explode('&', $getfield));
    foreach ($getfields as $g) {
      $split = explode('=', $g);

      /** In case a null is passed through **/
      if (isset($split[1])) {
        $oauth[$split[0]] = urldecode($split[1]);
      }
    }
  }
  $postfields = $this
    ->getPostfields();
  if (!is_null($postfields)) {
    foreach ($postfields as $key => $value) {
      $oauth[$key] = $value;
    }
  }
  $base_info = $this
    ->buildBaseString($url, $requestMethod, $oauth);
  $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
  $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  $oauth['oauth_signature'] = $oauth_signature;
  $this->url = $url;
  $this->requestMethod = $requestMethod;
  $this->oauth = $oauth;
  return $this;
}