Skip to content

Instantly share code, notes, and snippets.

@jtwiest
Last active December 21, 2016 00:42
Show Gist options
  • Save jtwiest/6dd04a8f30fd2dc874e2bf8f47bba40c to your computer and use it in GitHub Desktop.
Save jtwiest/6dd04a8f30fd2dc874e2bf8f47bba40c to your computer and use it in GitHub Desktop.
Twillio Connector
<?php
class Twillio
{
public static $sid = '';
public static $token = '';
public static $from = '';
/*
* Make outgoing call to specified number
*
* @params string $to
* @params string $callbackUrl
* @params array $options
* @return array
*/
public static function makeCall($to, $callbackUrl, $options = [])
{
$fields = [
'To' => $to,
'From' => static::$from,
'Url' => $callbackUrl,
];
$fields = array_merge($fields, $options);
return static::makeRequest('Calls', $fields);
}
/*
* Make outgoing call to specified number
*
* @params string $to
* @params string $body
* @params string $from
* @params array $options
* @return array
*/
public static function sendSMS($to, $body, $from = null, $options = [])
{
$fields = [
'To' => $to,
'From' => static::$from,
'Body' => $body
];
$fields = array_merge($fields, $options);
return static::makeRequest('SMS/Messages', $fields);
}
public static function makeRequest($path, $fields)
{
$auth = static::$sid . ':' . static::$token;
$uri = 'https://api.twilio.com/2010-04-01/Accounts/' . static::$sid . '/' . $path;
$fieldsSring = '';
foreach ($fields as $key => $val) {
$fieldsString .= '&' . $key . '=' . urlencode($val);
}
$res = curl_init();
curl_setopt($res, CURLOPT_URL, $uri);
curl_setopt($res, CURLOPT_POST, count($fields));
curl_setopt($res, CURLOPT_POSTFIELDS, $fieldsString);
curl_setopt($res, CURLOPT_USERPWD, $auth);
curl_setopt($res, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($res);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment