Class 'Twilio\Rest\Client' not found

Neo Algeria picture Neo Algeria · Jan 28, 2017 · Viewed 9.1k times · Source

I'm trying to use twilio php api .

This is my code:

        <?php
    $sid = "xxxxxx"; 
    $token = "xxxxxxx"; 

    $phone=$_POST["phone"];
    $code=$_POST["code"];

    $client = new Twilio\Rest\Client($sid, $token);
    $message = $client->messages->create(
      $phone, 
      array(
        'from' => 'xxxxxxx', 

   'body' => $code
  ));

It gives me this error :

Fatal error: Class 'Twilio\Rest\Client' not found in /home/vhosts/xxxx.xxxx.com/twilio/sms.php on line 9

I've also tried this code and didn't work:

     <?php
    $sid = "xxxxxxx"; 
    $token = "xxxxxxxx"; 


    require_once "Twilio/autoload.php";
        use Twilio\Rest\Client;

      $phone=$_POST["phone"];
       $code=$_POST["code"];

    $client = new Client($sid, $token);
    $message = $client->messages->create(
  $phone, 
  array(
    'from' => 'xxxxx', 
    'body' => $code
  ));

It gives me this error :

Fatal error: require(): Failed opening required '/home/vhosts/xxxx.xxxxx.com/twilio/Twilio/Version.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/vhosts/xxxx.xxxx.com/twilio/Twilio/autoload.php on line 140

Answer

Isabelle picture Isabelle · Apr 19, 2017

put this line at the very beginning:

use Twilio\Rest\Client;

then add your include statement:

require_once "Twilio/autoload.php";