Skip to content

Commit

Permalink
Merge pull request #149 from reedmaniac/4.x
Browse files Browse the repository at this point in the history
Twilio 4.x
  • Loading branch information
hannesvdvreken authored Apr 23, 2018
2 parents 783a74f + cd1949e commit 0fd74d5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 54 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ $twilio->call('+18085551212', function ($message) {
});
```

Access the configured `\Services_Twilio` object:
Access the configured `Twilio\Rest\Client` object:

```php
$sdk = $twilio->getTwilio();
Expand All @@ -116,15 +116,15 @@ $sdk = Twilio::getTwilio();

##### Pass as many optional parameters as you want

If you want to pass on extra optional parameters to the `messages->sendMessage(...)` method [from the Twilio SDK](https://twilio-php.readthedocs.io/en/latest/api/rest.html#Services_Twilio_Rest_Messages::sendMessage), you can do so
If you want to pass on extra optional parameters to the `messages->sendMessage(...)` method [from the Twilio SDK](https://www.twilio.com/docs/api/messaging/send-messages), you can do so
by adding to the `message` method. All arguments are passed on, and the `from` field is prepended from configuration.

```php
$twilio->message($to, $message, $mediaUrls, $params);
// passes all these arguments on.
```

The same is true for the [call method](https://twilio-php.readthedocs.io/en/latest/api/rest.html#Services_Twilio_Rest_Calls::create).
The same is true for the [call method](https://www.twilio.com/docs/api/voice/call#post-parameters).

```php
$twilio->call($to, $message, $params);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "MIT",
"require": {
"php": ">=5.5.0",
"twilio/sdk":">=3.12.0,<5.0.0"
"twilio/sdk":"5.*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^1.9",
Expand Down
5 changes: 0 additions & 5 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ public function from($connection)

$settings = $this->settings[$connection];

if (isset($settings['ssl_verify'])) {
return new Twilio($settings['sid'], $settings['token'], $settings['from'], $settings['ssl_verify']);
}

// Let the Twilio constructor decide the default value for ssl_verify
return new Twilio($settings['sid'], $settings['token'], $settings['from']);
}

Expand Down
67 changes: 34 additions & 33 deletions src/Twilio.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
namespace Aloha\Twilio;

use Services_Twilio;
use Services_Twilio_TinyHttp;
use Services_Twilio_Twiml;
use Twilio\Rest\Client;
use Twilio\Twiml;

class Twilio implements TwilioInterface
{
Expand All @@ -26,9 +25,9 @@ class Twilio implements TwilioInterface
* @var bool
*/
protected $sslVerify;

/**
* @var \Services_Twilio
* @var \Twilio\Rest\Client
*/
protected $twilio;

Expand All @@ -49,63 +48,65 @@ public function __construct($sid, $token, $from, $sslVerify = true)
/**
* @param string $to
* @param string $message
* @param array|null $mediaUrls
* @param array $params
*
* @link https://www.twilio.com/docs/api/messaging/send-messages Documentation
*
* @return \Services_Twilio_Rest_Message
* @return \Twilio\Rest\Api\V2010\Account\MessageInstance
*/
public function message($to, $message)
public function message($to, $message, $mediaUrls = null, array $params = [])
{
$arguments = func_get_args();
$options['body'] = $message;

array_unshift($arguments, $this->from);
if (!isset($options['from'])) {
$options['from'] = $this->from;
}

if (!empty($medialUrls)) {
$options['mediaUrl'] = $mediaUrls;
}

return call_user_func_array([$this->getTwilio()->account->messages, 'sendMessage'], $arguments);
return $this->getTwilio()->messages->create($to, $options);
}

/**
* @param string $to
* @param string|callable $message
*
* @return \Services_Twilio_Rest_Call
* @link https://www.twilio.com/docs/api/voice/making-calls Documentation
*
* @return \Twilio\Rest\Api\V2010\Account\CallInstance
*/
public function call($to, $message)
public function call($to, $message, array $params = [])
{
$arguments = func_get_args();

array_unshift($arguments, $this->from);

if (is_callable($message)) {
$query = http_build_query([
'Twiml' => $this->twiml($message),
]);

$arguments[2] = 'https://twimlets.com/echo?'.$query;
$message = 'https://twimlets.com/echo?'.$query;
}

return call_user_func_array([$this->getTwilio()->account->calls, 'create'], $arguments);
$params['url'] = $message;

return $this->getTwilio()->calls->create(
$to,
$this->from,
$params
);
}

/**
* @return \Services_Twilio
* @return \Twilio\Rest\Client
*/
public function getTwilio()
{
if ($this->twilio) {
return $this->twilio;
}

if (!$this->sslVerify) {
$http = new Services_Twilio_TinyHttp(
'https://api.twilio.com',
[
'curlopts' => [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
],
]
);
}

return $this->twilio = new Services_Twilio($this->sid, $this->token, null, isset($http) ? $http : null);
return $this->twilio = new Client($this->sid, $this->token);
}

/**
Expand All @@ -115,7 +116,7 @@ public function getTwilio()
*/
private function twiml(callable $callback)
{
$message = new Services_Twilio_Twiml();
$message = new Twiml();

call_user_func($callback, $message);

Expand Down
12 changes: 0 additions & 12 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@
*/

'from' => getenv('TWILIO_FROM') ?: '',

/*
|--------------------------------------------------------------------------
| Verify Twilio's SSL Certificates
|--------------------------------------------------------------------------
|
| Allows the client to bypass verifying Twilio's SSL certificates.
| It is STRONGLY advised to leave this set to true for production environments.
|
*/

'ssl_verify' => true,
],
],
],
Expand Down

0 comments on commit 0fd74d5

Please sign in to comment.