TwilioDevEd / clicktocall-php

Click to Call with Twilio and Vanilla PHP
https://www.twilio.com/docs/voice/sdks/javascript/get-started
MIT License
9 stars 7 forks source link

Getting "Credentials are required to create a Client" when submitting the form #10

Closed mmenschig closed 1 year ago

mmenschig commented 3 years ago

Version of PHP:

> php --version 
PHP 8.0.10 (cli) (built: Aug 26 2021 15:36:17) ( NTS )

Composer.json modification (I've added PHP ^8.0 as a supported version)

    "require": {
        "php": "^7.2.5|^8.0",
        "twilio/sdk": "^6.1.0",

Expectation: A call will go through from your Twilio number to the provided phone number.

Actual behavior: An exception is thrown.

[Wed Sep 15 13:43:03 2021] PHP Fatal error:  Uncaught Twilio\Exceptions\ConfigurationException: Credentials are required to create a Client in /Users/mmenschig/Projects/dev/php/clicktocall-php/vendor/twilio/sdk/src/Twilio/Rest/Client.php:170
Stack trace:
#0 /Users/mmenschig/Projects/dev/php/clicktocall-php/src/call.php(36): Twilio\Rest\Client->__construct('', '')
#1 {main}
  thrown in /Users/mmenschig/Projects/dev/php/clicktocall-php/vendor/twilio/sdk/src/Twilio/Rest/Client.php on line 170
[Wed Sep 15 13:43:03 2021] [::1]:60740 [200]: POST /call.php - Uncaught Twilio\Exceptions\ConfigurationException: Credentials are required to create a Client in /Users/mmenschig/Projects/dev/php/clicktocall-php/vendor/twilio/sdk/src/Twilio/Rest/Client.php:170
Stack trace:
#0 /Users/mmenschig/Projects/dev/php/clicktocall-php/src/call.php(36): Twilio\Rest\Client->__construct('', '')
#1 {main}
  thrown in /Users/mmenschig/Projects/dev/php/clicktocall-php/vendor/twilio/sdk/src/Twilio/Rest/Client.php on line 170
[Wed Sep 15 13:43:03 2021] [::1]:60740 Closing

The issue appears to stem from the following lines in call.php: Link: https://github.com/TwilioDevEd/clicktocall-php/blob/15d16f2ecef9624b1aa74f35ada525f9d86100cb/src/call.php#L30-L33

Note that both getenv() and putenv() are not thread safe and their usage is strongly discouraged: https://github.com/vlucas/phpdotenv#putenv-and-getenv

One workaround is to access the $_ENV object instead:

    $client = new Client(
        $_ENV["TWILIO_ACCOUNT_SID"],
        $_ENV["TWILIO_AUTH_TOKEN"]
    );

NB: The same workaround needs to be applied to retrieving the $TWILIO_NUMBER as well: $TWILIO_NUMBER = $_ENV['TWILIO_NUMBER'];

Once these changes are applied the application works as expected.

gigo6000 commented 2 years ago

I had the same issue, I'm also using php 8.0 thanks for the workaround.

mmenschig commented 2 years ago

@dkundel FYI - do you think this can get addressed sometime down the line?