From: mmikitka Date: Fri, 3 May 2013 04:25:40 +0000 (-0400) Subject: Added the Services_Twilio PEAR-compatible library. See CRM-12399 for more information. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=734872cd9734ff3465fd3e6e5aadc47335484231;p=civicrm-core.git Added the Services_Twilio PEAR-compatible library. See CRM-12399 for more information. ---------------------------------------- * CRM-12399: Integrate official Twilio PHP library with org.civicrm.sms.twilio http://issues.civicrm.org/jira/browse/CRM-12399 --- diff --git a/tools/extensions/org.civicrm.sms.twilio/org_civicrm_sms_twilio.php b/tools/extensions/org.civicrm.sms.twilio/org_civicrm_sms_twilio.php index 07fb7fde5e..c23155547c 100644 --- a/tools/extensions/org.civicrm.sms.twilio/org_civicrm_sms_twilio.php +++ b/tools/extensions/org.civicrm.sms.twilio/org_civicrm_sms_twilio.php @@ -25,6 +25,9 @@ +--------------------------------------------------------------------+ */ +// Load the official Twilio library +require_once 'Services/Twilio.php'; + /** * * @package CRM @@ -46,12 +49,6 @@ class org_civicrm_sms_twilio extends CRM_SMS_Provider { */ protected $_providerInfo = array(); - /** - * Curl handle resource id - * - */ - protected $_ch; - public $_apiURL = "https://api.twilio.com/"; protected $_messageType = array( @@ -60,6 +57,12 @@ class org_civicrm_sms_twilio extends CRM_SMS_Provider { 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 @@ -83,18 +86,18 @@ class org_civicrm_sms_twilio extends CRM_SMS_Provider { $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(); @@ -134,52 +137,41 @@ class org_civicrm_sms_twilio extends CRM_SMS_Provider { 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; } } @@ -192,39 +184,4 @@ class org_civicrm_sms_twilio extends CRM_SMS_Provider { $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); - } }