Created
July 30, 2013 00:26
-
-
Save 5iDS/6109093 to your computer and use it in GitHub Desktop.
Clickatell HTTP API Class
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
/*--------------------------------------------------*/ | |
/* SMS CLICKATELL | |
* | |
* Http API | |
* | |
* @param string user | |
* @param string password | |
* @param int api_id | |
* @param int/"array" to (cellphone number) / "","","" | |
* @param string text (sms message) | |
* | |
* | |
* http://api.clickatell.com/http/sendmsg?user=&password=&api_id=&to=&text= | |
* | |
* | |
/*--------------------------------------------------*/ | |
class sms_notifications { | |
private $user; | |
private $appID; | |
private $password; | |
private $session_id; | |
#----------------------------------------------------------------- | |
# INITIALISE ACCOUNT INFO | |
# | |
# | |
# @param int $appID | |
# @param string $user | |
# @param string $password | |
# | |
#----------------------------------------------------------------- | |
public function __construct( $appID, $user, $password) { | |
$this->appID = $appID; | |
$this->user = $user; | |
$this->password = $password; | |
} | |
#----------------------------------------------------------------- | |
# INITIATE SESSION ID | |
# | |
# @return string session_id | |
#----------------------------------------------------------------- | |
public function ini_session_id () { | |
$_appID = $this->appID; | |
$_user = $this->user; | |
$_password = $this->password; | |
$url = 'https://api.clickatell.com/http/auth'; | |
$fields = array( | |
'api_id' => urlencode($_appID), | |
'user' => urlencode($_user), | |
'password' => urlencode($_password) | |
); | |
$session_request = $this->http_req( 'POST', $url, $fields ); | |
$session_response = explode(":",$session_request[0]); | |
if ($session_response[0] == "OK") { | |
$this->session_id = trim($session_response[1]); | |
} else { | |
$this->session_id = false; | |
} | |
return trim($this->session_id); | |
} | |
#----------------------------------------------------------------- | |
# SEND SMS | |
# | |
# User CURL to send sms message to multiple recipients. | |
# | |
# @param string text (sms message) | |
# @param array maximum of 100 recipients / batch for GET request | |
# 300 for POST request | |
#----------------------------------------------------------------- | |
public function send_sms($message, $recipients) { | |
if( empty($message) || empty($recipients) ) { | |
throw new Exception('Please ensure you\'ve provided a message as well as recipients.'); | |
} elseif ( count($recipients) > 300 ) { | |
throw new Exception('Too many recipients for Batch.'); | |
} | |
$url = "http://api.clickatell.com/http/sendmsg"; | |
//MAKE SURE SPACES ARE REPLACED WITH '+' IN MESSAGE CONTENT | |
$message = $this->plusarize($message); | |
//SETUP FEEDBACK | |
$delivery_report = array(); | |
if( is_array($recipients) || is_object($recipients) ) { | |
foreach ($recipients as $recipient) { | |
$fields = array( | |
'session_id' => urlencode($this->session_id), | |
'to' => urlencode($recipient), | |
'text' => urlencode($message) | |
); | |
$delivery_report[] = $this->http_req( 'POST', $url, $fields ); | |
} | |
} else { | |
$fields = array( | |
'session_id' => urlencode($this->session_id), | |
'to' => urlencode($recipients), | |
'text' => $message | |
); | |
$delivery_report[] = $this->http_req( 'POST', $url, $fields ); | |
} | |
return $delivery_report; | |
} | |
#----------------------------------------------------------------- | |
# Find 'spaces' within string and replace them with '+' | |
# @param string | |
# @return string | |
#----------------------------------------------------------------- | |
private function plusarize($text) { | |
//$text = str_replace(' ', '+', $text); | |
$text = preg_replace('/\s+/', '+', $text); | |
return $text; | |
} | |
#----------------------------------------------------------------- | |
# Url-lify data for post request | |
# @param array | |
# @return string | |
#----------------------------------------------------------------- | |
private function url_lify($data) { | |
foreach($data as $key=>$value) { | |
$fields_string .= $key.'='.$value.'&'; | |
} | |
return rtrim($fields_string, '&'); | |
} | |
#----------------------------------------------------------------- | |
# MAKE AN HTTP REQUEST | |
# @param string text to find @name | |
# @return string | |
# | |
#----------------------------------------------------------------- | |
private function http_req( $method, $url, $data ) { | |
/**/ | |
$fields_string = $this->url_lify($data); | |
$url = $url .'?'. $fields_string; | |
$response = file($url); | |
/** / | |
//open connection | |
$ch = curl_init(); | |
switch ( strtoupper($method) ) { | |
case 'POST': | |
curl_setopt($ch,CURLOPT_POST, count($data)); | |
if (!empty($data)) { | |
$fields_string = $this->url_lify($data); | |
//$fields_string = http_build_query($data) . "\n"; | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); | |
} | |
break; | |
} | |
//set the url, number of POST vars, POST data | |
curl_setopt($ch,CURLOPT_URL, $url); | |
//execute post | |
$response = curl_exec($ch); | |
//close connection | |
curl_close($ch); | |
/**/ | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment