Created
April 2, 2013 14:11
-
-
Save Opus1no2/5292500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Example | |
*/ | |
class Survo_To_Sms | |
{ | |
/** | |
* @var array $_IvrOptions | |
*/ | |
private $_IvrOptions = array(); | |
/** | |
* @var array $_ApiOptions | |
*/ | |
private $_ApiOptions = array( | |
'action' => 'sms.send_message', | |
'api_key' => 'YOURAPIKEY' | |
); | |
const ENDPOINT = 'ibp_api.php?'; | |
const BASE = 'http://secure.ifbyphone.com/'; | |
/** | |
* | |
* Class construct | |
* | |
* @param array $options | |
* | |
* @return void | |
*/ | |
public function __construct(array $options) | |
{ | |
$this->_setOpts($options); | |
} | |
/** | |
* | |
* Set options from IVR | |
* | |
* @param array $options | |
* | |
* @return void | |
*/ | |
private function _setOpts($options) | |
{ | |
if ($options['answer'] == 1) { | |
$this->_IvrOptions['answer'] = (int)$options['answer']; | |
} | |
if (strlen((int)$options['cid'] == 10)) { | |
$this->_IvrOptions['cid'] = (int)$options['cid']; | |
} | |
} | |
/** | |
* | |
* Set SMS message | |
* | |
* @param string $message | |
* | |
* @return void | |
*/ | |
public function setMessage($message) | |
{ | |
$this->_ApiOptions['message'] = (string)$message; | |
} | |
/** | |
* | |
* Set SMS from number | |
* | |
* @param int $from | |
* | |
* @return void | |
*/ | |
public function setFrom($from) | |
{ | |
$this->_ApiOptions['message'] = (int)$from; | |
} | |
/** | |
* | |
* Set SMS to number | |
* | |
* @param int $to | |
* | |
* @return void | |
*/ | |
public function setTo($to) | |
{ | |
$this->_ApiOptions['to'] = (int)$to; | |
} | |
/** | |
* | |
* Process business rules | |
* | |
* @return mixed | |
*/ | |
private function _processLogic() | |
{ | |
if ($this->_IvrOtions['answer']) { | |
$this->_request(); | |
} | |
} | |
/** | |
* | |
* Perform web request | |
* | |
* @return mixed | |
*/ | |
private function _request() | |
{ | |
$response = file_get_contents($this->_getUrl()); | |
return $this->_process($response); | |
} | |
/** | |
* | |
* Assemple API url for request | |
* | |
* @return string | |
*/ | |
private function _getUrl() | |
{ | |
$url = self::BASE . self::ENDPOINT . http_build_query($this->_ApiOptions); | |
return (string) $url; | |
} | |
/** | |
* | |
* Process web request result | |
* | |
* @param string $response | |
* | |
* @return xml | |
* | |
* @throws RunTimeException | |
*/ | |
private function _process($response) | |
{ | |
$xml = simplexml_load_string($response); | |
if ($xml->result == 'failed') { | |
throw new RunTimeException(sprintf( | |
'Invalid result: [%s]', $xml->description | |
)); | |
} else { | |
return $xml; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment