+--------------------------------------------------------------------+
*/
+// Load the official Twilio library
+require_once 'Services/Twilio.php';
+
/**
*
* @package CRM
*/
protected $_providerInfo = array();
- /**
- * Curl handle resource id
- *
- */
- protected $_ch;
-
public $_apiURL = "https://api.twilio.com/";
protected $_messageType = array(
protected $_messageStatus = array(
);
+ /**
+ * Twilio client object
+ * @var Service_Twilio
+ */
+ protected $_twilioClient = null;
+
/**
* We only need one instance of this object. So we use the singleton
* pattern and cache the instance in this variable
$this->_apiType = CRM_Utils_Array::value('api_type', $provider, 'http');
$this->_providerInfo = $provider;
- if ($skipAuth) {
- return TRUE;
+ // Instantiate the Twilio client
+ if ($this->_apiType == 'http' &&
+ array_key_exists('username', $this->_providerInfo) &&
+ array_key_exists('password', $this->_providerInfo)
+ ) {
+ $sid = $this->_providerInfo['username'];
+ $token = $this->_providerInfo['password'];
+ $this->_twilioClient = new Services_Twilio($sid, $token);
}
- // first create the curl handle
-
- /**
- * Reuse the curl handle
- */
- $this->_ch = curl_init();
- if (!$this->_ch || !is_resource($this->_ch)) {
- return PEAR::raiseError('Cannot initialise a new curl handle.');
+ if ($skipAuth) {
+ return TRUE;
}
$this->authenticate();
return (TRUE);
}
- function formURLPostData($url, $id = NULL) {
- $url = $this->_providerInfo['api_url'] . $url;
- $postData = array();
- return array($url, $postData);
- }
-
/**
* Send an SMS Message via the Twilio API Server
*
* @param array the message with a to/from/text
*
- * @return mixed true on success or PEAR_Error object
+ * @return mixed SID on success or PEAR_Error object
* @access public
*/
function send($recipients, $header, $message, $jobID = NULL) {
- if ($this->_apiType = 'http') {
- list($url, $postData) = $this->formURLPostData("/2010-04-01/Accounts/{$this->_providerInfo['username']}/SMS/Messages.xml");
- $auth = $this->_providerInfo['username'] . ':' . $this->_providerInfo['password'];
+ if ($this->_apiType == 'http') {
+ $from = '';
if (array_key_exists('From', $this->_providerInfo['api_params'])) {
- $postData['From'] = $this->_providerInfo['api_params']['From'];
- }
- $postData['To'] = $header['To'];
- $postData['Body'] = $message;
-
- $response = $this->curl($url, $postData, $auth);
-
- if (PEAR::isError($response)) {
- return $response;
+ $from = $this->_providerInfo['api_params']['From'];
}
-
- $send = simplexml_load_string($response['data']);
- $sid = $send->SMSMessage->Sid;
- if (!empty($sid)) {
- $this->createActivity($sid, $message, $header, $jobID);
- return $sid;
- }
- else {
- $errMsg = $send->RestException->Message
+
+ try {
+ $twilioMessage = $this->_twilioClient->account->sms_messages->create(
+ $from,
+ $header['To'],
+ $message
+ );
+ } catch (Exception $e) {
+ $errMsg = $e->getMessage()
. ' For more information, see '
- . $send->RestException->MoreInfo;
+ . $e->getInfo();
return PEAR::raiseError(
$errMsg,
- null,
+ $e->getCode(),
PEAR_ERROR_RETURN
);
}
+
+ $sid = $twilioMessage->sid;
+ $this->createActivity($sid, $message, $header, $jobID);
+ return $sid;
}
}
$fromPhone = $this->retrieve('From', 'String');
return parent::processInbound($fromPhone, $this->retrieve('Body', 'String'), NULL, $this->retrieve('SmsSid', 'String'));
}
-
- /**
- * Perform curl stuff
- *
- * @param string URL to call
- * @param string HTTP Post Data
- * @param string Authorization string composed of Account SID and Secret Key
-
- * @return mixed HTTP/XML response body or PEAR Error Object
- * @access private
- */
- function curl($url, $postData, $auth) {
- $this->_fp = tmpfile();
- curl_setopt($this->_ch, CURLOPT_URL, $url);
- curl_setopt($this->_ch, CURLOPT_POST, 1);
- curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($this->_ch, CURLOPT_USERPWD, $auth);
- curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
-
- $status = curl_exec($this->_ch);
-
- $response['http_code'] = curl_getinfo($this->_ch, CURLINFO_HTTP_CODE);
-
- if (empty($response['http_code'])) {
- return PEAR::raiseError('No HTTP Status Code was returned.', null, PEAR_ERROR_RETURN);
- }
- elseif ($response['http_code'] === 0) {
- return PEAR::raiseError('Cannot connect to the Twilio API Server.', null, PEAR_ERROR_RETURN);
- }
-
- $response['data'] = $status;
- asort($response);
-
- return ($response);
- }
}