Skip to content

Instantly share code, notes, and snippets.

@jeremeamia
Last active May 15, 2018 21:45
Show Gist options
  • Save jeremeamia/748a8dd3317893ab842b to your computer and use it in GitHub Desktop.
Save jeremeamia/748a8dd3317893ab842b to your computer and use it in GitHub Desktop.
The beginnings of a Guzzle-based, PHP Twilio client.
{
"require": {
"guzzlehttp/guzzle": "~4.2",
"guzzlehttp/guzzle-services": "~0.3.0",
"guzzlehttp/retry-subscriber": "~1.0"
},
"authors": [
{
"name": "Jeremy Lindblom",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Twilio\\": "src/"
}
}
}
<?php
require 'vendor/autoload.php';
$twilio = new Twilio\Client([
'account_sid' => 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
]);
$result = $twilio->sendMessage([
'From' => '+15551784133',
'To' => '+15559449534',
'Body' => 'Hello, world!',
]);
echo "Message SID: {$result['sid']}\n";
print_r($result->toArray());
<?php return [
'baseUrl' => 'https://api.twilio.com',
'apiVersion' => '2010-04-01',
'operations' => [
'SendMessage' => [
'httpMethod' => 'POST',
'uri' => '/{ApiVersion}/Accounts/{AccountSid}/Messages.json',
'responseModel' => 'Resource',
'parameters' => [
'AccountSid' => [
'required' => true,
'type' => 'string',
'location' => 'uri',
],
'ApiVersion' => [
'required' => true,
'type' => 'string',
'location' => 'uri',
],
'From' => [
'required' => true,
'type' => 'string',
'location' => 'postField',
],
'To' => [
'required' => true,
'type' => 'string',
'location' => 'postField',
],
'Body' => [
'type' => 'string',
'location' => 'postField',
],
'MediaUrl' => [
'type' => 'string',
'location' => 'postField',
],
'StatusCallback' => [
'type' => 'string',
'location' => 'postField',
],
'ApplicationSid' => [
'type' => 'string',
'location' => 'postField',
],
],
],
],
'models' => [
'Resource' => [
'type' => 'object',
'additionalProperties' => [
'location' => 'json',
]
],
]
];
<?php
namespace Twilio;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Collection;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Model;
use GuzzleHttp\Subscriber\Retry\RetrySubscriber;
/**
* Web service client for Twilio
*
* @method Model sendMessage(array $config = [])
*/
class Client extends GuzzleClient
{
/**
* @param array $config
*/
public function __construct(array $config = [])
{
// Create a collection object with defaults and required params set
$config = Collection::fromConfig($config, [
'max_retries' => 3,
'description_path' => __DIR__ . '/twilio-api.php'
], ['account_sid', 'auth_token']);
// Ensure that the HTTP client and Service Description
$this->handleHttpClientOptions($config);
$this->handleDescriptionOptions($config);
// Create the Twilio Client
parent::__construct(
$config['http_client'],
$config['description'],
$config->toArray()
);
// Ensure that the credentials are set
$this->handleCredentialsOptions($config);
// Ensure that ApiVersion is set
$this->setConfig(
'defaults/ApiVersion',
$this->getDescription()->getApiVersion()
);
}
/**
* @param Collection $config
*/
private function handleHttpClientOptions(Collection $config)
{
if ($config['http_client']) {
// HTTP client was injected
return;
}
$client = new HttpClient($config['http_client_options'] ?: []);
// Attach request retry logic
$client->getEmitter()->attach(new RetrySubscriber([
'max' => $config['max_retries'],
'filter' => RetrySubscriber::createChainFilter([
RetrySubscriber::createStatusFilter(),
RetrySubscriber::createCurlFilter(),
]),
]));
$config['http_client'] = $client;
}
/**
* @param Collection $config
*
* @throws \InvalidArgumentException
*/
private function handleDescriptionOptions(Collection $config)
{
if ($config['description']) {
// Service description was injected
return;
}
// Load service description data
$path = $config['description_path'];
$data = file_exists($path) ? include $path : null;
if (!is_array($data)) {
throw new \InvalidArgumentException('Invalid description');
}
$config['description'] = new Description($data);
}
/**
* @param Collection $config
*/
private function handleCredentialsOptions(Collection $config)
{
if (!$this->getHttpClient()->getDefaultOption('auth')) {
$this->getHttpClient()->setDefaultOption('auth', [
$config['account_sid'],
$config['auth_token'],
]);
}
$this->setConfig('defaults/AccountSid', $config['account_sid']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment