问题描述:

I've been working with Rachet WebSockets and created a simple chat application. The example uses a WebSocket namespace. This is my first time using namespace. Now I'm trying to add Twilio service but can seem to add Twilio to my namespace.

I know it is autoloaded in the autoload_files.php

<?php

// autoload_files.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));

$baseDir = dirname($vendorDir);

return array(

$vendorDir . '/twilio/sdk/Services/Twilio.php',

);

In the composer.json file

{

"autoload": {

"psr-0": {

"Websocket": "src"

}

},

"require": {

"cboden/ratchet": "^0.3.3",

"twilio/sdk": "^4.5"

}

}

I followed the steps from this website : https://www.twilio.com/docs/libraries/php#using-without-composer

I'm calling twilio inside a method of my class like this:

$AccountSid = "xxxxxxxxxxxxxxxxx";

$AuthToken = "xxxxxxxxxxxxxxxxx";

$client = new Client($sid, $token);

$message = $client->account->messages->create(array(

'To' => "+555555555",

'From' => "+555555555",

'Body' => "This is a test",

));

Keep getting this error: Uncaught Error: Class 'Websocket\Client' not found in ......

I'm very new to composer and namespace, hope this is enough information to help me.

网友答案:

When using namespaces, PHP will always start looking for classes which aren't prepended with their own namespace in the current one.

In your case the current namespace would be Websocket, thus PHP is trying to autoload the class Websocket\Client, to prevent this, you have two options :

1) Tell PHP where to look by using use :

use Twilio\Rest\Client;

2) Prepend the correct namespace

$client = new \Twilio\Rest\Client($sid, $token);
网友答案:

I had to update Twilio,

ran composer require twilio/sd

- Removing twilio/sdk (4.12.0)
- Installing twilio/sdk (5.4.1)
Downloading: 100%         

Now I'm able to use Twilio\Rest\Client; since it was missing before.

相关阅读:
Top