You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the `ConnectionManager`, you can quickly create calls and send SMS messages through Twilio's REST API using their PHP SDK. By default, the `ConnectionManager` fulfills the `TwilioClient` contract and can be used by injecting the service into your class/method, retrieving the service from the container, or using the `TwilioClient` facade.
28
+
29
+
```php
30
+
namespace App\Http\Controllers;
31
+
32
+
use BabDev\Twilio\ConnectionManager;
33
+
use BabDev\Twilio\Contracts\TwilioClient;
34
+
use Illuminate\Http\Request;
35
+
36
+
final class MessagingController
37
+
{
38
+
private $twilio;
39
+
40
+
public function __construct(ConnectionManager $twilio)
41
+
{
42
+
$this->twilio = $twilio;
43
+
}
44
+
45
+
public function myConstructorAction()
46
+
{
47
+
// Fetch a connection from the manager and send a message
48
+
$connection = $this->twilio->connection();
49
+
$connection->message('+15555555555', 'My test message');
50
+
51
+
// Or, send a message through the manager using the default connection
52
+
$this->twilio->message('+15555555555', 'My test message');
53
+
}
54
+
55
+
public function myInjectedAction(Request $request, TwilioClient $twilio)
56
+
{
57
+
// This is the default connection from your manager
58
+
$twilio->message('+15555555555', 'My test message');
59
+
}
60
+
61
+
public function myAppAction()
62
+
{
63
+
/** @var ConnectionManager $twilio */
64
+
$twilio = app(ConnectionManager::class);
65
+
$twilio->message('+15555555555', 'My test message');
66
+
}
67
+
68
+
public function myFacadeAction()
69
+
{
70
+
// The facade uses the connection manager, so you may retrieve any connection through it
71
+
\TwilioClient::connection()->message('+15555555555', 'My test message');
72
+
}
73
+
}
74
+
```
75
+
76
+
## Multiple Connections
77
+
78
+
If your application uses multiple sets of REST API credentials for the Twilio API, you can add the data for multiple connections to the package's configuration. First, you should publish this package's configuration:
Then, in your newly created `config/twilio.php` file, you can add new connections to the `connections` array. Note, the default "twilio" connection has been created for you and uses environment variables by default. You are free to change the default connection for your application and the name of the "twilio" connection if desired.
85
+
86
+
```php
87
+
<?php
88
+
89
+
return [
90
+
'default' => env('TWILIO_CONNECTION', 'twilio'),
91
+
92
+
'connections' => [
93
+
'twilio' => [
94
+
'sid' => env('TWILIO_API_SID', ''),
95
+
'token' => env('TWILIO_API_AUTH_TOKEN', ''),
96
+
'from' => env('TWILIO_API_FROM_NUMBER', ''),
97
+
],
98
+
99
+
'my_new_connection' => [
100
+
'sid' => 'SID-1',
101
+
'token' => 'TOKEN-1',
102
+
'from' => 'PHONE-1',
103
+
],
104
+
],
105
+
];
106
+
```
107
+
108
+
## Customizing Client Creation
109
+
110
+
You can customize the setup of `TwilioClient` instances using the `ConnectionManager::extend()` method, this allows you to define a custom callback to be used for creating a client. You may either override the creation of a named connection from your configuration, or dynamically create a new connection.
111
+
112
+
```php
113
+
<?php
114
+
115
+
namespace App\Providers;
116
+
117
+
use App\Twilio\TwilioClient;
118
+
use BabDev\Twilio\Contracts\TwilioClient as TwilioClientContract;
119
+
use Illuminate\Contracts\Container\Container;
120
+
use Illuminate\Support\ServiceProvider;
121
+
use Twilio\Http\Client as HttpClient;
122
+
use Twilio\Rest\Client as RestClient;
123
+
124
+
class AppServiceProvider extends ServiceProvider
125
+
{
126
+
/**
127
+
* Register any application services.
128
+
*
129
+
* @return void
130
+
*/
131
+
public function register()
132
+
{
133
+
\TwilioClient::extend(
134
+
'custom',
135
+
function (Container $container): TwilioClientContract {
136
+
/*
137
+
* Create a custom client from your application.
138
+
*
139
+
* For your convenience, you can use the Laravel container to create the
140
+
* Twilio\Rest\Client SDK class and its internal Twilio\Http\Client dependency
This package attempts to create an appropriate `Twilio\Http\Client` for the SDK based on the packages available in your application, using the following preferences:
165
+
166
+
- If using Laravel 7 and Guzzle is available, a client using [Laravel's HTTP client](https://laravel.com/docs/http-client) is used
167
+
- If Guzzle is available, the Twilio SDK's Guzzle client is used
168
+
- If neither of the above criteria are met, the Twilio SDK's Curl client is used
169
+
170
+
If you need to customize the HTTP client used by default in your application, you can extend the "`Twilio\Http\Client`" service this package creates to use your own HTTP client.
0 commit comments