4

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

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Neo Algeria
  • 235
  • 2
  • 3
  • 13
  • Do you use an autoloader (ie the one you get with composer)? – JimL Jan 28 '17 at 16:12
  • no i'm not using composer i'm using the twilio-php source code directly – Neo Algeria Jan 28 '17 at 16:15
  • You need some kind of autoloader so PHP will know where to find the Twilio files. I strongly suggest just installing twilio-php using composer as that will set up everything for you – JimL Jan 28 '17 at 16:16
  • Try to include the php class or autoloader if there is any – Thanh Trung Jan 28 '17 at 16:21
  • i've tried that please look at the main post to see the error – Neo Algeria Jan 28 '17 at 16:34
  • I respect that you may have your reasons for not using an autoloader. However, given the sheer number of classes in the Twilio SDK, you're asking for a lot of frustration and unwanted effort not using Composer. – Matthew Setter Jul 03 '23 at 09:54

3 Answers3

9

put this line at the very beginning:

use Twilio\Rest\Client;

then add your include statement:

require_once "Twilio/autoload.php";
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Isabelle
  • 91
  • 1
  • 3
0

Have You Tried to point to the Twilio Rest Client File?

in my case:

include __DIR__ . "/vendor/twilio/sdk/src/Twilio/Rest/Client.php";
-1

May You have not Installed Twilio SDK.
Run this command to install twilio/sdk

composer require twilio/sdk

Ref: https://www.twilio.com/blog/real-time-sms-order-notifications-magento-twilio-php

  • Install the Twilio SDK Composer Dependency
qɐʇǝɥɐW
  • 347
  • 3
  • 17