abraham/twitteroauth

random_int() expects exactly 2 arguments, 0 given

kfranke opened this issue · 0 comments

What are you attempting to do
Attempting to get started, verify account credentials

Expected behavior
successful connection and account verification

Actual behavior
Php throws a fatal error that backtraces to the use of random_int() function.
phps random_int function requires 2 parameters https://www.php.net/manual/en/function.random-int

Fatal error: Uncaught ArgumentCountError: random_int() expects exactly 2 arguments, 0 given in /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/Request.php:285
Stack trace:
#0 /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/Request.php(285): random_int()
#1 /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/Request.php(62): Abraham\TwitterOAuth\Request::generateNonce()
#2 /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/TwitterOAuth.php(577): Abraham\TwitterOAuth\Request::fromConsumerAndToken(Object(Abraham\TwitterOAuth\Consumer), Object(Abraham\TwitterOAuth\Token), 'POST', 'https://api.twi...', Array, true)
#3 /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/TwitterOAuth.php(537): Abraham\TwitterOAuth\TwitterOAuth->oAuthRequest('https://api.twi...', 'POST', Array, true)
#4 /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/TwitterOAuth.php(489): Abraham\TwitterOAuth\TwitterOAuth->makeRequests('https://api.twi...', 'POST', Array, true)
#5 /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/TwitterOAuth.php(254): Abraham\TwitterOAuth\TwitterOAuth->http('POST', 'https://api.twi...', 'tweets', Array, true)
#6 /Users/kfranke/Downloads/twitter/TwitterTransport.php(31): Abraham\TwitterOAuth\TwitterOAuth->post('tweets', Array, true)
#7 {main}
  thrown in /Users/kfranke/Downloads/twitter/vendor/twitteroauth/src/Request.php on line 285

Code example

<?php

require "vendor/twitteroauth/autoload.php";
require "vendor/ca-bundle/src/CaBundle.php";
use Abraham\TwitterOAuth\TwitterOAuth;
use Composer\CaBundle\CaBundle;

// Get everything you need from the dev.twitter.com/apps page
$consumer_key = TWITTER_APP_KEY;
$consumer_secret = TWITTER_APP_SECRET;
$access_token = TWITTER_TOKEN;
$access_token_secret = TWITTER_TOKEN_SECRET;

// Initialize the connection
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$connection->setApiVersion('2'); //1.1 default
$status_payload = ['text' => 'felixlive.com alert ' . time(),];
$response = $connection->post('tweets', $status_payload, true);

Versions:

  • TwitterOAuth: v4.0.0
  • PHP: v8.1.8

Additional context
you can resolve the error by providing the function with min max values are required in Request.php

 public static function generateNonce(): string
    {
        return md5(microtime() . random_int(-1000,1000));
    }