0

I'm trying to use Twilio's API to send a sms with PHP. I followed the tutorial as it is presented on their website but it doesn't work. I correctly replaced the $sid, $token, "from number" and "to number" variables. I also tested it with Phyton and it worked correctly, but with PHP I get the following error:

Fatal error: Uncaught Twilio\Exceptions\ConfigurationException: Credentials are required to create a Client in C:\xampp\htdocs\Php\Twilio\vendor\twilio\sdk\src\Twilio\Rest\Client.php:172 Stack trace: #0 C:\xampp\htdocs\Php\Twilio\index.php(13): Twilio\Rest\Client->__construct('', '') #1

I'm using PHP 7.3.21 and xampp. I've saw this same error with people using laravel and more complex codes, but I'm using "pure" PHP. The code I'm using:

<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once 'vendor/autoload.php';

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$message = $twilio->messages
                  ->create("+15558675310", // to
                           [
                               "body" => "This is the ship that made the Kessel Run in fourteen parsecs?",
                               "from" => "+15017122661"
                           ]
                  );

print($message->sid);

You can find the tutorial that I followed here: https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-php

  • check your `$sid` and `$token` if they have the correct values – André Walker Nov 08 '21 at 21:32
  • @B001ᛦ I've also sent them a ticket – SmukasPlays77 Nov 08 '21 at 21:37
  • @AndréWalker I did it and it's correct. I used their python code with the same $sid and $token values and worked well. – SmukasPlays77 Nov 08 '21 at 21:38
  • 1
    Are you sure that `getenv("TWILIO_ACCOUNT_SID")` is returning the account sid? Same question for the auth token. – philnash Nov 08 '21 at 23:50
  • @philnash apparently that's the problem. It's returning an empty string. I tried to add both variables following this: https://phoenixnap.com/kb/windows-set-environment-variable but it's still not working. – SmukasPlays77 Nov 09 '21 at 01:57
  • But you were able to set the environment variables for your Python version? Are you running the PHP inside some sort of container that is setting its own environment variables? – philnash Nov 09 '21 at 05:29
  • Yeah, I was able to set them with Python. I fixed it after restarting my PC some times and now the environment variables are working. Thanks for the tip. – SmukasPlays77 Nov 09 '21 at 11:31

1 Answers1

1

It was a problem with the environment variables. I hardcoded them but the getenv() was returning an empty string and I don't know why it was happening. So to fix it I set them following this tutorial (for windows): phoenixnap.com/kb/windows-set-environment-variable. For linux you can try using these commands:

export ACCOUNT_SID="YOUR_ACCOUNT_SID"
export AUTH_TOKEN="YOUR_AUTH_TOKEN"

And then, in your code, for windows and linux:

$sid = getenv('ACCOUNT_SID');
$token = getenv('AUTH_TOKEN');

Make sure to restart xampp (if you're using it) and your computer after setting your variables.