'Services_Twilio' not found - Laravel

As far as I understand, I cannot simply use use Twilio to make it work. Thus, I tried require_once and require. The path should also be correct

I tried using require_once

    $twilioDir = '../vendor/twilio/sdk/Services/Twilio.php';
    require_once($twilioDir);

    $client = new Services_Twilio($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);

Class 'App\Http\Controllers\Services_Twilio' not found

What am I doing wrong?


Also, using require gave me error:

Cannot redeclare Services_Twilio_autoload() (previously declared in /var/www/Laravel/vendor/twilio/sdk/Services/Twilio.php:9)

I tried adding false to the line spl_autoload_register('Services_Twilio_autoload', false); in Twilio.php, but no luck


As far as I understand, I cannot simply use use Twilio to make it work.

Correct, because the class is named Services_Twilio.

use Services_Twilio; should do the trick.

Laravel handles autoloading for you. You shouldn't need to manually require the library unless Twilio has goofed something in their Composer setup.

I don't know who told you that you can't use Twilio but you can certainly use Twilio.

  1. Grab the composer package - composer require aloha/twilio
  2. Register the ServiceProvider in app.php like any other vendor: 'Aloha\Twilio\Support\Laravel\ServiceProvider', should be added to the providers array.
  3. Register the facade to make life easy - in app.php add to the aliases array: 'Twilio' => 'Aloha\Twilio\Support\Laravel\Facade',
  4. (optional) run php artisan vendor:publish so you can manage the assets that the vendor exposes to you.
  5. Because we previously added the facade to the aliases array in our app.php we can correctly use Twilio; within our classes.
  6. If you didn't do 3, then you need to reference the full namespace path; use Aloha\Twilio\Support\Laravel\Facade which will give you access to Twilio:: inside of that particular file.

Edit

I should note that you do not use Twilio from within the class, you must reference it outside of the class and before the class.

use Twilio; //Aloha\Twilio\Support\Laravel\Facade

class MyController {
    /**
     * Now you can use Twilio::whatever
     */
}

You have to import it at the top of the file.

I am not sure about the full path to the file but I am guessing it should be something like:

use Twilio\SDK\Services\Twilio

Update: Follow the guide here: https://github.com/aloha/laravel-twilio

The accepted answer didn't work for me.

Background: My code had worked for a while with use Services_Twilio;, but then I stopped using Twilio for months (or maybe more than a year), and then I was getting this error.

What ended up working was updating my code to say use Twilio\Rest\Client; instead and then create the client object via new Client($this->config['account_sid'], $this->config['auth_token']);.

This seems to be Twilio's newer way of doing things.

These docs helped.

Comments
  • I used composer require twilio/sdk. Did I download the wrong thing from composer?
  • @senty I believe that you are my friend. What you downloaded was actually just the standalone SDK or Twilio. What aloha twilio does is provide you the same functionality but with Laravel (4/5) integration.
  • I installed the right one. Now I started getting error: Call to undefined method Aloha\Twilio\Support\Laravel\Facade::message() :/
  • @senty Doesn't sound like you registered the ServiceProvider correctly.
  • But it's should be called Services_Twilio, no?
  • Yeah, whatever the path is.