【问题标题】:Class 'Twilio\Rest\Client' not found找不到类“Twilio\Rest\Client”
【发布时间】:2017-06-14 04:40:36
【问题描述】:

我正在尝试使用 twilio php api。

这是我的代码:

        <?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
  ));

它给了我这个错误:

致命错误:在第 9 行的 /home/vhosts/xxxx.xxxx.com/twilio/sms.php 中找不到类“Twilio\Rest\Client”

我也试过这段代码,但没有用:

     <?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
  ));

它给了我这个错误:

致命错误:require(): 无法打开所需的 '/home/vhosts/xxxx.xxxxx.com/twilio/Twilio/Version.php' (include_path='.:/usr/share/pear:/usr/share /php') 在 /home/vhosts/xxxx.xxxx.com/twilio/Twilio/autoload.php 第 140 行

【问题讨论】:

  • 您是否使用自动加载器(即您使用 composer 获得的)?
  • 不,我没有使用 composer 我直接使用 twilio-php 源代码
  • 您需要某种自动加载器,以便 PHP 知道在哪里可以找到 Twilio 文件。我强烈建议使用 composer 安装 twilio-php,因为它会为你设置好一切
  • 尝试包含php类或自动加载器(如果有)
  • 我试过了,请看主帖看看错误

标签: php twilio-php


【解决方案1】:

把这一行放在最开始:

use Twilio\Rest\Client;

然后添加您的包含语句:

require_once "Twilio/autoload.php";

【讨论】:

  • 很好的解决方案,为我节省了很多时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2017-12-24
  • 1970-01-01
相关资源
最近更新 更多